https://leetcode.com/problems/valid-palindrome/
-----------------------------------------------------------
See what's Palindrome here. Basically means that you can read forwards or backward and you get the same thing, like: level.
Two pointers.
Transform all into lower case first, then process one by one.
//////////////////////////////////////////////////////////////////
// codes
class Solution {
public:
bool isPalindrome(string s) {
int len = s.size(), l = 0, r = len - 1;
//check
input
//if
(len <= 1) return true;
//transform
to the lower case
for (int i = 0; i < len; i++){
if (s[i] >= 'A' && s[i] <= 'Z')
s[i] = s[i] + 32;
}
while (l<r){
while (l<len){
if (s[l] >= 'a' && s[l] <= 'z' || s[l] >= '0' && s[l] <= '9')break;
else l++;
if (l == len)return true;
}
while (r>-1){
if (s[r] >= 'a' && s[r] <= 'z' || s[r] >= '0' && s[r] <= '9')break;
else r--;
//if(r==-1)return true;
}
if (s[l] != s[r])return false;
l++;
r--;
}
return true;
}
};
No comments:
Post a Comment