C++ solution by using iterator


  • 0
    A
    class Solution {
    public:
        bool isPalindrome(string s) {
            if (s.empty() | (s.find_first_not_of(" ") == -1) | (s.size() == 1))
    		    return true;
    	    else {
    		    auto beg = s.cbegin(), end = s.cend() - 1;
    		    while (beg <= end) {
    			    if (!isalnum(*beg) | !isalnum(*end)) {
    				    if (!isalnum(*beg))
    					    ++beg;
    				    if (!isalnum(*end))
    					    --end;
    			    }
    			    else {
    				    if (tolower(*beg) == tolower(*end)) {
    					    ++beg;
    					    --end;
    				    }
    				    else {
    					    return false;
    				    }
    			    }
    		    }
    		    return true;
    	    }
        }
    };
    

Log in to reply
 

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