您的位置:首页 > 其它

【题解】【字符串】【Leetcode】Valid Palindrome

2014-02-01 18:00 417 查看
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.

思路:

这题不能更简单了,考察的是徒手写bug的能力= =

代码:

bool isPalindrome(string s) {
int n = s.length();
int i = 0;
int j = n-1;
while(i<j){//while(i>j){//蠢哭了
while(!isalnum(s[i]) && i<n) i++;//没加i<n会超时,不看题用isalpha()
while(!isalnum(s[j]) && j>=0) j--;
if(i<j && tolower(s[i++]) !=  tolower(s[j--])) //if(s[i++] !=  s[j--] && i>j)//自加自减最坑了, 不看题没用tolower()
return false;
}
return true;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: