-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.java
42 lines (38 loc) · 1.17 KB
/
Solution.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
42
package _009;
/**
* <pre>
* author: Blankj
* blog : http://blankj.com
* time : 2017/04/24
* desc :
* </pre>
*/
public class Solution {
// public boolean isPalindrome(int x) {
// if (x < 0) return false;
// int copyX = x, reverse = 0;
// while (copyX > 0) {
// reverse = reverse * 10 + copyX % 10;
// copyX /= 10;
// }
// return x == reverse;
// }
public boolean isPalindrome(int x) {
if (x < 0 || (x != 0 && x % 10 == 0)) return false;
int halfReverseX = 0;
while (x > halfReverseX) {
halfReverseX = halfReverseX * 10 + x % 10;
x /= 10;
}
return halfReverseX == x || halfReverseX / 10 == x;
}
public static void main(String[] args) {
Solution solution = new Solution();
System.out.println(solution.isPalindrome(-1));
System.out.println(solution.isPalindrome(10010));
System.out.println(solution.isPalindrome(0));
System.out.println(solution.isPalindrome(11));
System.out.println(solution.isPalindrome(111));
System.out.println(solution.isPalindrome(222222222));
}
}