-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
44 lines (34 loc) · 874 Bytes
/
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
43
44
/**
* @author: changle
* @time: 2019-06-11 21:32
* source: https://leetcode.com/problems/palindrome-number/
*/
public class Solution {
public static boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
long reverse = 0;
int primaryX = x;
int curBit;
for (int i = getLengthOfInteger(x) - 1; i >= 0; i--) {
curBit = x % 10;
x /= 10;
reverse += curBit * Math.pow(10, i);
}
return reverse == primaryX ;
}
static int getLengthOfInteger(int x) {
// x 的长度
int lengthOfX = 0;
int temp = x;
while (temp > 0) {
temp /= 10;
lengthOfX++;
}
return lengthOfX;
}
public static void main(String[] args) {
System.out.println(isPalindrome(121));
}
}