-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq009_PalindromeNumber.java
More file actions
39 lines (33 loc) · 941 Bytes
/
q009_PalindromeNumber.java
File metadata and controls
39 lines (33 loc) · 941 Bytes
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
package leetcode_algorithm;
public class q009_PalindromeNumber {
public static void main(String[] args) {
System.out.println(isPalindrome(1233221));
System.out.println(isPalindrome(123321));
System.out.println(isPalindrome(-123321));
System.out.println(isPalindrome(1));
System.out.println(isPalindrome(10));
System.out.println(isPalindrome(-10));
}
/**
* ½â·¨1
* @param x
* @return
*/
public static boolean isPalindrome(int x) {
if(x < 0)
return false;
if(x/10 == 0)
return true;
int[] temp = new int[String.valueOf(x).length()];
int i = 0;
while (x != 0) {
temp[i++] = x % 10;
x = x / 10;
}
for( i = 0;i<temp.length;i++) {
if(temp[i] != temp[temp.length - i - 1])
return false;
}
return true;
}
}