forked from nightcore420/learn-for-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Palindrome Number
51 lines (40 loc) · 908 Bytes
/
Palindrome Number
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
45
46
47
48
49
50
51
public class Palindrome_Number {
public static void main(String[] args) {
Palindrome_Number out = new Palindrome_Number();
Solution s = out.new Solution();
System.out.println(s.isPalindrome(12321));
System.out.println(s.isPalindrome(Integer.MAX_VALUE));
}
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0) {
return false;
}
long rev = 0;
int y = x;
while(y > 0) {
int remainder = y % 10;
y /= 10;
rev = rev * 10 + remainder;
}
if(rev > Integer.MAX_VALUE) {
return false;
}
return ((int) rev) == x;
}
}
public class Solution2 {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int xorig = x;
int rev = 0;
while(x > 0) {
rev = rev * 10 + x % 10;
x /= 10;
}
return rev == xorig;
}
}
}