-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSolution125.java
More file actions
46 lines (40 loc) · 1.13 KB
/
Solution125.java
File metadata and controls
46 lines (40 loc) · 1.13 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
package algorithm.leetcode;
/**
* @author: mayuan
* @desc: 验证回文串
* @date: 2019/01/03
*/
public class Solution125 {
public static void main(String[] args) {
String str = "OP";
String str2 = "A man, a plan, a canal: Panama";
System.out.println(str.toLowerCase());
System.out.println(new Solution125().isPalindrome(str));
System.out.println(new Solution125().isPalindrome(str2));
}
public boolean isPalindrome(String s) {
if (null == s) {
return false;
}
if (0 == s.length()) {
return true;
}
String text = s.toLowerCase();
int i = 0, j = text.length() - 1;
while (i <= j) {
if ('a' > text.charAt(i) || 'z' < text.charAt(i)) {
++i;
} else if ('a' > text.charAt(j) || 'z' < text.charAt(j)) {
--j;
} else {
if (text.charAt(i) == text.charAt(j)) {
++i;
--j;
} else {
return false;
}
}
}
return true;
}
}