Solutions to high-frequency interview questions of LeetCode in C++17, taking into account both efficiency and comprehensibility.
abcddba
| |
l r
abcddba
| |
l r
abcddba
| |
l r
不等,因此不是回文串
class Solution {
public:
bool isPalindrome(string s) {
int l = 0;
int r = size(s) - 1;
while (l < r) {
while (l < r && !isalnum(s[l])) {
++l;
}
while (l < r && !isalnum(s[r])) {
--r;
}
if (toupper(s[l]) == toupper(s[r])) {
++l;
--r;
} else {
return false;
}
}
return true;
}
};