Leetcode 680 Valid Palidrone II

题意:

判断一个字符串最多删除1个字符依旧能得到回文字符串

https://leetcode.com/problems/valid-palindrome-ii/?source=submission-noac

cpp 复制代码
class Solution {
public:
    bool validPalindrome(string s) {
        int l = 0; int r = s.size()-1;
        while(l < r) {
            if(s[l] != s[r]) {
                int newl = l + 1;
                int newr = r - 1;
                return valid(s, newl, r) || valid(s, l, newr);
            }
            l++;
            r--;
        }
        return true;
    }

    bool valid(string& s, int l, int r) {
        while(l < r) {
            if(s[l] != s[r]) {
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
};
相关推荐
1白天的黑夜114 分钟前
栈-844.比较含退格的字符串-力扣(LeetCode)
c++·leetcode·
Swift社区2 小时前
LeetCode 378 - 有序矩阵中第 K 小的元素
算法·leetcode·矩阵
墨染点香2 小时前
LeetCode 刷题【73. 矩阵置零】
算法·leetcode·矩阵
林木辛3 小时前
LeetCode热题 438.找到字符中所有字母异位词 (滑动窗口)
算法·leetcode
dragoooon343 小时前
[优选算法专题二——NO.16最小覆盖子串]
c++·算法·leetcode·学习方法
1白天的黑夜16 小时前
栈-1047.删除字符串中的所有相邻重复项-力扣(LeetCode)
c++·leetcode·
im_AMBER6 小时前
Leetcode 18 java
java·算法·leetcode
愚润求学8 小时前
【贪心算法】day9
c++·算法·leetcode·贪心算法
songx_9911 小时前
leetcode29( 有效的括号)
java·数据结构·算法·leetcode