题目:
代码(首刷自解 2024年4月4日):
class Solution {
public:
bool isPalindrome(string s) {
int left = 0,right = s.size() - 1;
unordered_set<char> upplette;
unordered_set<char> lowLetter;
unordered_set<char> digit;
for (auto i = '0'; i <= '9'; ++i) digit.insert(i);
for (auto i = 'a' ; i <= 'z'; ++i) lowLetter.insert(i);
for (auto i = 'A' ; i <= 'Z'; ++i) upplette.insert(i);
while (left < right) {
// 跳过非字母
while (left < right && lowLetter.find(s[left]) == lowLetter.end() && upplette.find(s[left]) == upplette.end() && digit.find(s[left]) == digit.end())
left++;
while (left < right && lowLetter.find(s[right]) == lowLetter.end() && upplette.find(s[right]) == upplette.end() && digit.find(s[right]) == digit.end())
right--;
if (left >= right) return true;
// 若是大写,变成小写 ascii + 32
if (upplette.find(s[left]) != upplette.end()) s[left] += 32;
if (upplette.find(s[right]) != upplette.end()) s[right] += 32;
// 不相等返回false
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
};