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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
bool (int x) {
if (x < 0)
return false;
if (x == 0)
return true;

int base = 1;
int n = x;
while (n /= 10)
base *= 10;

n = x;
while (n) {
int left = n / base;
int right = n % 10;
if (left != right)
return false;

n -= base * left;
n /= 10;
base /= 100;
}
return true;
}
};