leetcode680 验证回文字符串

class Solution {
public:
	bool validPalindrome(string s) {
		if (s.empty()) return true;
		if (s.size() == 1) return true;
		int start = 0;
        int end = s.size()-1;
        while (start < end) {
            if (s[start] == s[end]) {
                start++;
                end--;
            }
            else {
                // 导致不能构成回文字符串的字符在左半部分
                //     i         j
                // a b e c d e d c b a 
                // 在[i+1,j]中构成了回文字符串
                
                //       i     j
                // a b c d e d z c b a 
                // 在 [i, j-1]中构成了回文字符串
                
                return (isHuWen(s,start+1, end) ||  isHuWen(s, start, end-1));
            }
        }
        return true;
    }
private:
    bool isHuWen(string s, int i, int j) {
        if (i > j) return false;
        while(i <= j) {
            if (s[i++] != s[j--])
                return false;
        }
        return true;
    }
        
};

猜你喜欢

转载自blog.csdn.net/weixin_36149892/article/details/80179386
今日推荐