Leetcode学习笔记:#125. Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
实现:
public boolean isPalindrome(String s){
if(s.isEmpty()){
return true;
}
int head = 0, tail = s.length() - 1;
char cHead, cTail;
while(head <= tail){
cHead = s.charAt(head);
cTail = s.charAt(tail);
if(!Character.isLetterOrDigit(cHead)){
head++;
}else if(!Character.isLetterOrDigit(cTail)){
tail--;
}else{
if(Character.toLowerCase(cHead) != Character.toLowerCase(cTail)){
return false;
}
head++;
tail--;
}
}
return true;
}
思路:
两个指针,一个从前往后 一个从后向前,每遍历一次,则判断一次两个指针是否为letter,是则判断两个指针的字符是否相等,不是返回true。