您的位置:首页 > 其它

leetcode - Valid Palindrome

2015-06-22 20:25 295 查看
leetcode - Valid Palindrome

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 isAlphanumeric(const char a){
if( (a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z') || (a >= '0' && a <= '9' ))
return true;
else
return false;
}
bool isPalindrome(string s) {
if(s.size()==0) return true;
string::iterator it_beg = s.begin();
string::iterator it_end = s.end();
//bool flag = true;
//while(!isAlphanumeric(*it_beg) && it_end!=s.end()) it_beg++;
//while(!isAlphanumeric(*it_end) && it_end!=s.begin()) it_end--;
while(it_beg < it_end){
if(!isAlphanumeric(*it_beg)) it_beg++;
else if(!isAlphanumeric(*it_end)) it_end--;
else if(*it_beg == *it_end || (*it_beg -*it_end == 'a' - 'A') || (*it_beg -*it_end == 'A' - 'a') ){
it_beg++;
it_end--;
}
else{
return false;
}
}
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: