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;
    }
};
相关推荐
Lenyiin1 小时前
《LeetCode 热题 100》整整 100 题量大管饱题解套餐 中
java·c++·python·leetcode·面试·刷题·lenyiin
蒟蒻小袁1 小时前
力扣面试150题--颠倒二进制位
java·算法·leetcode
1 小时前
LeetCode Hot 100 括号生成
算法·leetcode·职场和发展
逝雪Yuki2 小时前
Leetcode——42. 接雨水
c++·算法·leetcode·双指针·接雨水
姜不吃葱3 小时前
【力扣热题100】哈希——两数之和
算法·leetcode·哈希算法·力扣热题100
一匹电信狗3 小时前
【C++】手搓一个STL风格的vector容器
c语言·数据结构·c++·算法·leetcode·stl·visual studio
逝雪Yuki4 小时前
Leetcode——11. 盛最多水的容器
c++·算法·leetcode·双指针
薰衣草233315 小时前
一天两道力扣(6)
算法·leetcode
逝雪Yuki15 小时前
Leetcode——287. 寻找重复数
c++·leetcode·二分查找·双指针·环形链表
科大饭桶16 小时前
数据结构自学Day13 -- 快速排序--“前后指针法”
数据结构·算法·leetcode·排序算法·c