Skip to content

Commit 76e450b

Browse files
authored
Merge pull request gzc426#68 from ChenforCode/master
验证回文串
2 parents 62a1e92 + 9d04ca6 commit 76e450b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

2018.11.27-leetcode125/ChenforCode.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author: ChenforCode
3+
* @date: 2018/11/26 14:29
4+
* @description: 验证回文串,只验证数字和字符
5+
*/
6+
public class Solution {
7+
public static void main(String[] args){
8+
System.out.println(isPalindrome("A man, a plan, a canal: Panama"));
9+
}
10+
11+
public static boolean isPalindrome(String s) {
12+
char[] chars = s.toCharArray();
13+
int i = 0, j = chars.length - 1;
14+
while (i < j){
15+
if (!Character.isLetterOrDigit(chars[i])){
16+
i++;
17+
} else if (!Character.isLetterOrDigit(chars[j])){
18+
j--;
19+
} else {
20+
if (Character.toLowerCase(chars[i]) == Character.toLowerCase(chars[j])){
21+
i++;
22+
j--;
23+
} else {
24+
return false;
25+
}
26+
}
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)