leetcode || 125、Valid Palindrome
problem:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
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.
Hide Tags
Two Pointers String
題意:1つの文字列が有効な返信文字列であるかどうかを判断し、数字と大文字と小文字のみを考慮し、他の記号は考慮しない.
thinking:
(1)二重ポインタを採用し、首尾ポインタを中間に回り込む
(2)まず文字をフィルタリングし、数字と大文字と小文字以外の記号を除外する
(2)文字が等しいか、大文字と小文字が悪いだけで、両方のポインタが真ん中にさらに進みます
code:
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.
Hide Tags
Two Pointers String
題意:1つの文字列が有効な返信文字列であるかどうかを判断し、数字と大文字と小文字のみを考慮し、他の記号は考慮しない.
thinking:
(1)二重ポインタを採用し、首尾ポインタを中間に回り込む
(2)まず文字をフィルタリングし、数字と大文字と小文字以外の記号を除外する
(2)文字が等しいか、大文字と小文字が悪いだけで、両方のポインタが真ん中にさらに進みます
code:
class Solution {
public:
bool isPalindrome(string s) {
int n=s.size();
if(n==0)
return true;
int i=0;
int j=n-1;
while(i<=j)
{
char a=s.at(i);
char b=s.at(j);
if(a<'0'||(a>'9'&&a<'A')||a>'z'||(a>'Z'&&a<'a'))
{
i++;
continue;
}
if(b<'0'||(b>'9'&&b<'A')||b>'z'||(b>'Z'&&b<'a'))
{
j--;
continue;
}
if(a==b || abs(a-b)=='a'-'A')
{
i++;
j--;
}
else
return false;
}
return true;
}
};