LeetCode 125. Valid Palindrome
Description
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring other 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.
class Solution {
public boolean isPalindrome(String s) {
if (s.isEmpty()) {
return true;
}
int lo = 0;
int hi = s.length() - 1;
char clo, chi;
while (lo <= hi) {
clo = s.charAt(lo);
chi = s.charAt(hi);
if (!Character.isLetterOrDigit(clo)) {
lo++;
}
else if (!Character.isLetterOrDigit(chi)) {
hi--;
}
else {
if (Character.toLowerCase(clo) != Character.toLowerCase(chi)) {
return false;
}
lo++;
hi--;
}
}
return true;
}
}
本文详细介绍了LeetCode上125题Valid Palindrome的解题思路及Java实现方法,通过忽略非字母数字字符并比较字符串两端元素的方式判断一个字符串是否为回文串。

176

被折叠的 条评论
为什么被折叠?



