forked from algorhythms/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
43 lines (40 loc) · 1.05 KB
/
Copy pathSolution.java
File metadata and controls
43 lines (40 loc) · 1.05 KB
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
package PalindromeNumber;
/**
* User: Danyang
* Date: 1/15/2015
* Time: 12:44
* Determine whether an integer is a palindrome. Do this without extra space.
*/
public class Solution {
/**
* TESTING
* OVERFLOW
* @param x
* @return
*/
public boolean isPalindrome(int x) {
if(x<0)
return false;
long sig = 1; // int overflow
for(int i=x; i>0; i/=10)
sig *= 10;
sig /= 10;
// for(int i=x; i>=10; i=i%(sig)/10, sig/=100) {
// if(i%10!=i/sig)
// return false;
// }
int acc = 10;
for(int i=x; sig>=acc; sig/=10, acc*=10) {
long l = i/sig%10;
long r = i%acc/(acc/10);
if(r!=l)
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(new Solution().isPalindrome(1000000001));
System.out.println(new Solution().isPalindrome(100031));
System.out.println(new Solution().isPalindrome(12321));
}
}