125. Valid Palindrome

https://leetcode.com/problems/valid-palindrome/

求给定字符串是否是回文字符串

双指针法

class Solution {
public:
    bool isPalindrome(string s) {
        int start=0,end=s.size()-1;
        while(start<end)
        {
           // 判断是不是字母或者数字
            while(start<end && !((s[start] >='a'&&s[start]<='z')|| (s[start] >= 'A'&&s[start]<='Z') || (s[start]>='0' && s[start]<='9')))
                start ++;
            while(start<end && !((s[end] >='a'&&s[end]<='z')|| (s[end] >= 'A'&& s[end]<='Z') || (s[end]>='0' && s[end]<='9')))
                end--;

            
            if(s[start]==s[end])
            {
                ++start;
                --end;
            }
            // 如果不相等,判断二者是不是都是字母,且一个是大写一个是小写
            // 注意 'A' 和 '!' 相差也是32,因此需要多加一个判断是不是字母 
            else if(s[start]>='a'&&s[start]<='z'&&(s[start]-s[end]==32))
            {
                ++start;
                --end;
            }
            else if(s[end]>='a'&&s[end]<='z'&&(s[end]-s[start]==32))
            {
                ++start;
                --end;
            }
            else
                return false;
        }
        return true;
        
    }
};
posted @ 2020-03-29 22:37  默写年华  阅读(71)  评论(0编辑  收藏  举报