您的位置:首页 > 其它

leetcode Valid Palindrome

2015-12-09 20:20 232 查看
原题链接:https://leetcode.com/problems/valid-palindrome/

Description

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.

class Solution {
public:
bool isPalindrome(string s) {
ret = "";
if (s.empty()) return true;
int n = (int)s.length();
for (int i = 0; i < n; i++) {
if (isalpha(s[i]) || isdigit(s[i])) ret += tolower(s[i]);
}
return isOk();
}
private:
string ret;
bool isOk() const {
int i = 0, j = (int)ret.length() - 1;
while (i < j) {
if (ret[i] != ret[j]) return false;
i++, j--;
}
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: