Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
“A man, a plan, a canal: Panama” is a palindrome.
“race a car” is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
可以忽略除数字和字母以外的标点符号。
public boolean isPalindrome(String s) {
if (s.length() == 0)
return true;
int i = 0, j = s.length() - 1;
while (i < j) {
while (i < j
&& !((s.charAt(i) >= 'a' && s.charAt(i) <= 'z')
|| (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') || (s
.charAt(i) >= '0' && s.charAt(i) <= '9')))
i++;
while (i < j
&& !((s.charAt(j) >= 'a' && s.charAt(j) <= 'z')
|| (s.charAt(j) >= 'A' && s.charAt(j) <= 'Z') || (s
.charAt(j) >= '0' && s.charAt(j) <= '9'))) {
System.out.println("--" + j);
j--;
}
if (!(s.charAt(i) + "").equalsIgnoreCase(s.charAt(j) + "")) {
return false;
}
i++;
j--;
}
return true;
}
这样写通过了。下面学习下别人的。
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
int front = 0;
int end = s.length() - 1;
while (front < end) {
while (front < s.length() && !isvalid(s.charAt(front))){ // nead to check range of a/b
front++;
}
if (front == s.length()) { // for emtpy string “.,,,”
return true;
}
while (end >= 0 && ! isvalid(s.charAt(end))) { // same here, need to check border of a,b
end--;
}
if (Character.toLowerCase(s.charAt(front)) != Character.toLowerCase(s.charAt(end))) {
break;
} else {
front++;
end--;
}
}
return end <= front;
}
private boolean isvalid (char c) {
return Character.isLetter(c) || Character.isDigit(c);
}
}