Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Be aware of overflow problem.
class Solution {
public:
bool isPalindrome(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if (x < 0) return false;
int digits = 1;
int tester = 1;
while (x / tester >= 10) {
tester*=10;
digits++;
}
while (x > 0) {
if (x / tester == x % 10) {
x -= x / tester * tester;
x /= 10;
tester /= 100;
}
else {
return false;
}
}
return true;
}
};
No comments:
Post a Comment