Valid Palindrome ---- LeetCode 125

时间:2022-07-29 11:21:14

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.

Solution 1:

class Solution
{
public:
bool isPalindrome(string s)
{
int left = , right = s.length() - ;
while(left < right)
{
char a = tolower(s[left]), b = tolower(s[right]);
if(!isalnum(a))
{
++left;
continue;
}
if(!isalnum(b))
{
--right;
continue;
}
if(a != b) return false; ++left;
--right;
}
return true; }
};