标签:
判断正整数是否是回文。不许额外占用空间。
【分析】
题目提示说,如果想把整数变换成string型,不符合题目不额外占空间,另外
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
所以只能利用数学计算,/和%分别算出每一位。
class Solution { public: bool isPalindrome(int x) { if(x<0) return false; int base=1; int left; int j=0,i=0; while(x/base>=10){ base*=10; j++; } left=base; while(x/left==x%10&&x>10){ i++; x%=left; x/=10; if(left>100) left/=100; } if(x<10&&i>=j/2) return true; else if(x==0) return true; else return false; } };
【总结】124ms
一开始想不到怎样获得最高位,每次除以10太繁琐,看了别人的求base才得到启示。
标签:
原文地址:http://www.cnblogs.com/ketchups-notes/p/4216262.html