forked from qiyuangong/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path009_Palindrome_Number.java
41 lines (40 loc) · 1003 Bytes
/
009_Palindrome_Number.java
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
31
32
33
34
35
36
37
38
39
40
41
class Solution {
public boolean isPalindrome(int x) {
if (x < 0)
return false;
int temp = x;
int len = 0;
while (temp != 0) {
temp /= 10;
len ++;
}
temp = x;
int left, right;
for (int i = 0; i < len / 2; i++) {
right = temp % 10;
left = temp / (int) Math.pow(10, len - 2 * i - 1);
left = left % 10;
if (left != right)
return false;
temp /= 10;
}
return true;
}
// Leetcode book
public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while ( x / div >= 10) {
div *= 10;
}
while (x !=0) {
int l = x / div;
int r = x % 10;
if (l != r) return false;
// Remove left and right number
x = (x % div) / 10;
div /= 100;
}
return true;
}
}