-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode9.java
More file actions
48 lines (42 loc) · 1.24 KB
/
LeetCode9.java
File metadata and controls
48 lines (42 loc) · 1.24 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
44
45
46
47
48
public class LeetCode9 {
public static void main(String[] args) {
// 输入:x = 121
// 输出:true
System.out.println(new Solution9().isPalindrome(121));
// 输入:x = -121
// 输出:false
System.out.println(new Solution9().isPalindrome(-121));
// 输入:x = 10
// 输出:false
System.out.println(new Solution9().isPalindrome(10));
}
}
class Solution9 {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
String s = Integer.toString(x);
return isPalindrome2(s, 0, s.length() - 1);
}
public boolean isPalindrome1(String s, int start, int end) {
int length = end - start;
if (length <= 0) {
return true;
} else {
if (s.charAt(start) == s.charAt(end)) {
return isPalindrome1(s, start + 1, end - 1);
} else {
return false;
}
}
}
public boolean isPalindrome2(String s, int start, int end) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {
return false;
}
}
return true;
}
}