forked from yuzhangcmu/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIsPalindrome.java
More file actions
executable file
·41 lines (34 loc) · 997 Bytes
/
Copy pathIsPalindrome.java
File metadata and controls
executable file
·41 lines (34 loc) · 997 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
40
41
package Algorithms.string;
public class IsPalindrome {
public boolean isPalindrome(String s) {
// http://blog.csdn.net/fightforyourdream/article/details/12860445
if (s == null) {
return false;
}
int len = s.length();
s = s.toLowerCase();
int l = 0;
int r = len - 1;
while (l < r) {
if (!isValid(s.charAt(l))) {
l++;
} else if (!isValid(s.charAt(r))) {
r--;
} else if (s.charAt(l) != s.charAt(r)) {
return false;
} else {
l++;
r--;
}
}
return true;
}
public boolean isValid(char c) {
return Character.isLetterOrDigit(c);
// if (c <= 'z' && c >= 'a' || c <= 'Z' && c >= 'A'
// || c <= '9' && c >= '0') {
// return true;
// }
// return false;
}
}