[LeetCode]Valid Palindrome

来源:互联网 发布:javascript游戏编程 编辑:程序博客网 时间:2024/04/28 12:59
class Solution {public:bool isPalindrome(string s) {// Start typing your C/C++ solution below// DO NOT write int main() functionTransmitString(s);for (int i = 0, j = s.size()-1; i <= j; ++i, --j){if(s[i] != s[j])return false;}return true;}void TransmitString( string& str ) {//note: do not let the string move too muchint i, j;for (i = 0, j = 0; i < str.size(); ++i){if(str[i] >= '0' && str[i] <= '9'){str[j++] = str[i];}else if (str[i] >= 'a' && str[i] <= 'z'){str[j++] = str[i];}else if (str[i] >= 'A' && str[i] <= 'Z'){str[j++] = str[i]-'A'+'a';}}str = str.substr(0, j);}};

second time

class Solution {public:    bool isValidChar(char c)    {        if('a' <= c && c <= 'z') return true;        if('A' <= c && c <= 'Z') return true;        if('0' <= c && c <= '9') return true;        return false;    }    char lowerCase(char c)    {        if('A' <= c && c <= 'Z') return c-'A'+'a';        else return c;    }    bool isPalindrome(string s) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        int l = 0;        int r = s.size()-1;        while(l < s.size() && !isValidChar(s[l])) l++;        while(r >= 0 && !isValidChar(s[r])) r--;                while(l < r)        {            if(lowerCase(s[l]) == lowerCase(s[r]))            {                l++;                while(l < s.size() && !isValidChar(s[l])) l++;                r--;                while(r >= 0 && !isValidChar(s[r])) r--;            }            else return false;        }        return true;    }};