class Solution {
public:
bool isPalindrome(string s) {
int st = 0, ed = s.length();
if (ed == 0) {
return true;
}
while (st < ed) {
if (!isalpha(s[st]) && !isdigit(s[st])) {
++st;
} else if(isalpha(s[ed]) || isdigit(s[ed])) {
if (toupper(s[st]) != toupper(s[ed])) {
break;
}
++st;
--ed;
} else {
--ed;
}
}
return st >= ed;
}
};

本文介绍了一个C++实现的算法,用于判断输入字符串是否为回文。该算法通过忽略非字母数字字符并转换为大写来比较字符串两端的字符。

被折叠的 条评论
为什么被折叠?



