Description:
Determine whether an integer is a palindrome. Do this without extra space.
Subscribe to see which companies asked this question.
判断是否是回文数,要求不使用额外的空间,则每次取头尾两个数判断它们是否相等,若相等删掉头尾的数。
Solution:
class Solution {
public:
bool isPalindrome(int x) {
if(x < 0)
return false;
int len = 1;
while(x / len >= 10)
len *= 10;
while(x > 0) {
int left = x / len;
int right = x % 10;
if(left != right)
return false;
else {
x = (x % len) / 10;
len /= 100;
}
}
return true;
}
};