palindrome number 代码

LeetCode上一道Easy题,目的是判断一个int类型的数是不是回文类型的,和前面判断一个字符串是不是回文的题是类似的,但是这个更简单,因为Int类型最大也就0x7fffffff,
不像字符串可以任意长度,太长了会超时。。。


代码

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
28
29
30
public boolean (int x) {
if (x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) {
return false;
}
long l = x;
boolean flag = false;
if (l < 0) {
return false;
}

int temp = 0;
String s = String.valueOf(l);

int i = 0;
int j = s.length() - 1;
while (i <= j) {
if(i == j)
return true;
if (s.charAt(i) == s.charAt(j)) {
if(i+1==j)
return true;
i++;
j--;
}
else
return false;
}
return false;

}