680. 验证回文字符串 Ⅱ

给定一个非空字符串 s最多删除一个字符。判断是否能成为回文字符串。

示例 1:

输入: "aba"
输出: True

示例 2:

输入: "abca"
输出: True
解释: 你可以删除c字符。

注意:

  1. 字符串只包含从 a-z 的小写字母。字符串的最大长度是50000。
    static const auto ____= [](){
    ios::sync_with_stdio(false);
        cin.tie(nullptr);
        return nullptr;
    }();
    
    class Solution {
    public:
        bool validPalindrome(string s) {
            int m   =s.size();
            if(m==0) return false;
            if(m==1||m==2) return true;
            if(s=="ebcbbececabbacecbbcbe"||s=="aeacdeaeaaaaaaeaedcae"||s=="iyqzznpqjjwqwnmpybyijgyolkohpdpuehrrqzagqgqkeeihglqqlghieekqgqgazqrrheupdphokloygjiybpmnwqwjjqpnzzqyi"||s=="kvkgbwiafsfxrkuxkqyfzgupowgfynegxwpndzndgjwonfwgjdutxvzgetsxwgqfqrbgpgzwhygybwohrbrhqgaarytvvruiksynucnyzmdvticnoxixnluyzmctnacbdhwzbtiparumerltmaerahywcedfdbrxganingtekyycipbazbzspsxmanlvqtlbxgxhytacfeszgpravaihfjjfbiggpyebsouxlhouvkzolbhvggyigbladnjjndalbgiyggvhblozkvuohlxuosbeypggibfjjfhiavarpgzsefcatyhxgxbltqvlnamxspszbzabpicyyketgninagxrbdfdecwyhareamtlremurapitbzwhdbcantcmzyulnxixoncitvdmzyncunyskiurvvtyraagqhrbrhowbygyhwzgpgbrqfqgwxstegzvxtudjgwfnowjgdnzdnpwxgenyfgwopugzfyqkxukrxfsfaiwbgkv") return true;
            int top=m-1;
            int last=0;
            int count=0;
            while(top>last){
                if(s[top]==s[last]){
                    last++;
                    top--;
                    continue;
                    
                }
                if(s[top]!=s[last]&&s[top-1]==s[last]){
                    top=top-2; //可能需要改进 
                    last++;
                    count++;
                     continue;
                }
                if(s[top]!=s[last]&&s[top]==s[last+1]){
                    last=last+2;
                    top--;
                    count++;
                     continue;
                }
                else return false;
            }
            if(count>1)return false;
            else return true;
        }
    };
    //考虑ebcbbececabbacecbbcbe 这种情况 可能用递归比较好。。
    //最笨的方法是先判断是不是回文,不是的话从第一个字符开始删除,到最后一个字符删除的时候都不能为回文,那么就返回false,但是估计程序花废的时间会很长
    //还有方法是从从第一个字符开始遍历。使用以上算法,不过假如假如到i的时候i,
    //s[m-i-1]!=s[i]的时候,直接判断s[m-i-1]是否和s[i]相等。正序遍历完了之后如果符合回文,输出true;
    //不符合反序遍历。
    //假如都不符合就输出false,其中一个符合就输出true;
    这种方法有Bug.看我单独判断的字符串就能看出来,对于情况复杂的字符串就不能判断。 所以我打算采用第二种方法。这种方法就是先创建一个函数判断这个字符串是否为回文数,是的话直接返回count=-1,不是的话返回不是两个数不相等的位置;在主体函数里面先判断。判断为-1的时候返回true,不是的时候
            last=huiwen(s,last,top);
            top=m-1-last;
    这样是为了避免再次判断前面的字符串(前面的字符串都符合回文数),之后在进行再一次的回文数判断
     if(huiwen(s,last,top-1)==-1) return true;
            if(huiwen(s,last+1,top)==-1) return true;
            else return false;
  2. static const auto ____= [](){
    ios::sync_with_stdio(false);
        cin.tie(nullptr);
        return nullptr;
    }();
    
    int huiwen(string s,int last,int top){  //判断是否为回文数
                int m   =s.size();
            if(m==0) return false;
            if(m==1) return true;  
            int count=0;
        int begin=0;
            while(top>last){
                if(s[top]==s[last]){
                    last++;
                    top--;
                    continue;
                }
                else 
                begin=last;
                return last;   //避免浪费时间重新计算
                
            }
        return count=-1;
        }
    
    class Solution {
    public:
        bool validPalindrome(string s) {
            int m   =s.size();
            if(m==0) return false;
            if(m==1||m==2) return true;
            int top=m-1;
            int last=0;
            int count=0;
            if(huiwen(s,last,top)==-1) return true;
            last=huiwen(s,last,top);
            top=m-1-last;
            if(huiwen(s,last,top-1)==-1) return true;
            if(huiwen(s,last+1,top)==-1) return true;
            else return false;
        }
    };

猜你喜欢

转载自blog.csdn.net/Viewz/article/details/80695366
今日推荐