您的位置:首页 > 其它

[LeetCode] Valid Palindrome

2015-07-10 20:40 375 查看
The suggested solution to this problem has given a clear idea. The tricky part of this problem is to handle all the edge cases carefully and write a clean code.

The following code should be self-explanatory. Note that the use of toupper avoid some messy if-else statements.

class Solution {
public:
bool isPalindrome(string s) {
int l = 0, r = s.length() - 1;
while (l < r) {
while (l < r && !isalnum(s[l])) l++;
if (l >= r) break;
while (r > l && !isalnum(s[r])) r--;
if (toupper(s[l++]) != toupper(s[r--]))
return false;
}
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: