首页 > 其他 > 详细

PO91回文字符串

时间:2021-09-15 18:23:09      阅读:20      评论:0      收藏:0      [点我收藏+]

技术分享图片

思路:和判断回文字符串差不多,双指针法

#include <string>
using namespace std;

class Solution
{
public:
    bool validPalindrome(string s)
    {
        return verify(s, 0, s.length() - 1, 0);
    }

    bool verify(string s, int i, int j, int diffcnt)
    {
        while (i < j)
        {
            if (s[i] == s[j])
            {
                i++;
                j--;
            }
            else
            {
                diffcnt++;
                if (diffcnt > 1)
                {
                    return false;
                }

                return verify(s, i + 1, j, diffcnt) || verify(s, i, j - 1, diffcnt);
            }
        }

        return true;
    }
};

int main(){
    Solution solution;
    string str;
    cin>>str;
    if(solution.validPalindrome(str)){
        cout<<"true"<<endl;
    }else{
        cout<<"false"<<endl;
    }
    return 0;
}

-[1]???offerⅡ019.最多删除一个字符得到回文题解

PO91回文字符串

原文:https://www.cnblogs.com/aoke2002/p/15270534.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!