A c++ solution


  • 0
    W
        bool isNum(char s){ return s>='0' && s<='9'; }
        bool isAlpha(char s){ return s>='a'&&s<='z'; }
        bool judge(const string& s){
            int size=s.size();
            //travese
            for(int i=0, j=size-1; i<j; ){
                /*skip ilegal char*/
                if(!isAlpha(s[i]) && !isNum(s[i])){
                    ++i;
                    continue;
                }
                if(!isAlpha(s[j]) && !isNum(s[j])){
                    --j;
                    continue;
                }
                
                //judge
                if( s[i]!=s[j] ) return false;
                ++i, --j;
            }
            return true;
        }
        bool isPalindrome(string s) {
            if(s=="") return true;
            
            //1.to lower
            for(int i=0, size=s.size(); i<size; i++){
                if(s[i]>='A'&&s[i]<='Z') s[i]=s[i]+32;
            }
            
            return judge(s);
        }

Log in to reply
 

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