125. Valid Palindrome

ss
Feb 17, 2021

--

檢查是不是回文, 但途中會有非字母或數字的字元, 要把它給略過, 另外大小寫一樣就可以通過了

class Solution {
public:
static bool isword(char c) {
if (c <= '9' && c >= '0')
return true;
if (c <= 'z' && c >= 'a') {
return true;
}
if (c <= 'Z' && c >= 'A')
return true;
return false;
}
bool isPalindrome(string s) {
int l = 0;
int r = s.size() - 1;
while (l < r) {
while (l < r && !isword(s[r])) {
r--;
}
while (l < r && !isword(s[l])) {
l++;
}
if (l < r && tolower(s[l]) != tolower(s[r])) {
return false;
}
r--;
l++;
}
return true;
}
};

--

--

ss
ss

No responses yet