A solution:Intuitive and easy to understand


  • 0
    Z

    Re: [Simple C++ O(1) space && O(n) time solution](without any "tricks")

    class Solution {
    public:
        bool isPalindrome(string s) {
            int len = s.size();
            if( len == 0) return true;
            int headp = 0,endp = len -1;
            while(headp < endp){
                if(s[headp] < '0' || (s[headp] > '9' && s[headp] < 'A') ||
                   (s[headp] > 'Z' && s[headp] < 'a') || s[headp] > 'z'){headp++; continue;}
                if(s[endp] < '0' || (s[endp] > '9' && s[endp] < 'A') ||
                   (s[endp] > 'Z' && s[endp] < 'a') || s[endp] > 'z'){endp--; continue;}
                   
                if(s[headp] >= '0' && s[headp] <= '9')
                    if(s[headp] != s[endp]) return false;
                    
                if(s[headp] >= 'A' && s[headp] <= 'Z')
                    if(s[headp] != s[endp] && s[headp] + ('a' - 'A') != s[endp]) return false;
    
                if(s[headp] >= 'a' && s[headp] <= 'z')    
                    if(s[headp] != s[endp] && s[headp] != (s[endp] + ('a' - 'A'))) return false;
                headp++;
                endp--;
            }
            return true;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.