forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidPalindrome.java
More file actions
28 lines (27 loc) · 732 Bytes
/
ValidPalindrome.java
File metadata and controls
28 lines (27 loc) · 732 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
public class ValidPalindrome {
/**
* 空串认为是true
*/
// 耗时10ms
public boolean isPalindrome(String s) {
/**
* 因为是忽略大小写,所以这里先转化成小写
*/
s = s.toLowerCase();
for (int i = 0, j = s.length() - 1; i < j; ) {
if (!Character.isLetterOrDigit(s.charAt(i))) {
i++;
} else if (!Character.isLetterOrDigit(s.charAt(j))) {
j--;
} else {
if (s.charAt(i) != s.charAt(j)) {
return false;
} else {
i++;
j--;
}
}
}
return true;
}
}