LeetCode 9: Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.

代码

1
2
3
4
5
6
7
8
9
10
11
12
bool isPalindrome(int x) {
if (x < 0 || (x!=0 && x%10==0)) {
return false;
}
int sum = 0;
while (x > sum) {
sum = sum*10 + x%10;
x /= 10;
}
return (x==sum)||(x==sum/10);
}
分享到 评论