题目:
Determine whether an integer is a palindrome. Do this without extra space.
解答:
//调优后的算法表现还不是很好,不知道还有没有更好的算法,暂时就这样吧!
//11506 / 11506 test cases passed.
//Status: Accepted
//Runtime: 56 ms
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) {
return false;
} else if (x < 10) {
return true;
}
int k[10] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
int j = -1;
for(int i = 9; i >= 0; --i) {
int e = x / k[i];
if (j < 0 && e > 0) {
j = 0;
}
if (j < 0) {
continue;
}
if (j >= i) {
return true;
}
int s = x / k[j];
if(j >= 0 && e % 10 != s % 10) {
return false;
}
++j;
}
return true;
}
};