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.
注意题目条件,字符数字字符,忽略大小写需要我们在判断的时候将两个字符都转为小写进行判断。
代码如下:
class Solution {
public:
bool isPalindrome(string s) {
int begin=0,end=s.size()-1;
while(begin < end)
{
while(!isalnum(s[begin]))
begin++;
while(!isalnum(s[end]))
end--;
if(begin<end && tolower(s[begin]) != tolower(s[end]))
return false;
begin++;end--;
}
return true;
}
};
本文介绍了一种算法,用于判断一个字符串是否为回文,只考虑字母数字字符并忽略大小写。通过双指针技巧实现,从两端向中间扫描,跳过非字母数字字符,将所有字符转换为小写进行比较。
1853

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



