美文网首页
125. Valid Palindrome

125. Valid Palindrome

作者: SilentDawn | 来源:发表于2018-06-28 15:28 被阅读0次

    Problem

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

    Note: For the purpose of this problem, we define empty string as valid palindrome.

    Example

    Input: "A man, a plan, a canal: Panama"
    Output: true
    
    Input: "race a car"
    Output: false
    

    Code

    static int var = [](){
        std::ios::sync_with_stdio(false);
        cin.tie(NULL);
        return 0;
    }();
    class Solution {
    public:
        bool isPalindrome(string s) {
            int i=0;
            int j=s.size()-1;
            transform(s.begin(),s.end(),s.begin(), ::tolower);
            while(i<j){
                if(!((s[i]>='a'&&s[i]<='z') || (s[i]>='0'&&s[i]<='9'))){
                    i++;
                    continue;
                }
                if(!((s[j]>='a'&&s[i]<='z') || (s[j]>='0'&&s[j]<='9'))){
                    j--;
                    continue;
                }
                if(s[i]==s[j]){
                    i++;
                    j--;
                    continue;
                }else{
                    return false;
                }
            }
            return true;
        }
    };
    

    Result

    125. Valid Palindrome.png

    相关文章

      网友评论

          本文标题:125. Valid Palindrome

          本文链接:https://www.haomeiwen.com/subject/dyigyftx.html