您的位置:首页 > 其它

leetcode-680-验证回文字符串

2019-09-07 16:46 190 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/ChenD17/article/details/100601569

 

class Solution {
public:
    bool validPalindrome(string s) {
        int left = 0, right = s.length() - 1, flag = 0, temp = 0;
        bool res_r=true, res_l=true;
        while (left <= right) {
            if (s[left] != s[right]) {
                if (flag == 1) res_r = false;
                else {
                    //right--;
                    temp = left;
                    left--;
                    flag = 1;
                }
            }    
            left++;
            right--;
        }
        if (res_r == true) return true;
        left = temp; 
        right = s.length() - 1 - left;
        flag = 0;
        while (left <= right) {
            if (s[left] != s[right]) {
                if (flag == 1) res_l = false;
                else {
                    //left++;
                    right++;
                    flag = 1;
                }
            }
            left++;
            right--;
        }
        return res_l;
    }
};

int main()
{
    Solution sol;
    string input = "deeee";
    cout << sol.validPalindrome(input) << endl;
    return 0;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: