LeetCode-Palindrome Number

Posted by mapoto4 on 2017-07-26

题目

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
public boolean isPalindrome(int x) {
if (x >= 2147483647 || x <= -2147483648)
return false;
if(x<0)
return false;
if(x<10)
return true;

int result = 0;
int result2 =x;

while (x > 0) {
if (result != 0 && Integer.MAX_VALUE / result < 10 && Integer.MAX_VALUE / result > -10)
return false;

result = result * 10 + x%10;
x = x / 10;
}
return result==result2;
}