LeetCode刷题实战125:验证回文串
共 865字,需浏览 2分钟
·
2020-12-16 21:13
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
Note: For the purpose of this problem, we define empty string as valid palindrome.
题意
示例 1:
输入: "A man, a plan, a canal: Panama"
输出: true
示例 2:
输入: "race a car"
输出: false
解题
public boolean isPalindrome(String s) {
if (s.isEmpty())
return true;
int begin = 0;
int end = s.length() - 1;
char beginChar, endChar;
while (begin <= end){
beginChar = s.charAt(begin);
endChar = s.charAt(end);
if (!Character.isLetterOrDigit(beginChar)){
begin++;
continue;
}
else if (!Character.isLetterOrDigit(endChar)){
end--;
continue;
}
else {
if (Character.toLowerCase(beginChar) != Character.toLowerCase(endChar))
return false;
else{
begin++;
end--;
}
}
}
return true;
}