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;
    }
};
相关推荐
flashlight_hi3 小时前
LeetCode 分类刷题:3217. 从链表中移除在数组中存在的节点
javascript·数据结构·leetcode·链表
Tisfy4 小时前
LeetCode 2536.子矩阵元素加 1:二维差分数组
算法·leetcode·矩阵
小欣加油5 小时前
leetcode 2536 子矩阵元素加1
数据结构·c++·算法·leetcode·矩阵
努力学算法的蒟蒻6 小时前
day14(11.14)——leetcode面试经典150
算法·leetcode
海琴烟Sunshine10 小时前
leetcode 383. 赎金信 python
python·算法·leetcode
cynicme16 小时前
力扣3228——将 1 移动到末尾的最大操作次数
算法·leetcode
熬了夜的程序员16 小时前
【LeetCode】109. 有序链表转换二叉搜索树
数据结构·算法·leetcode·链表·职场和发展·深度优先
Miraitowa_cheems19 小时前
LeetCode算法日记 - Day 102: 不相交的线
数据结构·算法·leetcode·深度优先·动态规划
Miraitowa_cheems19 小时前
LeetCode算法日记 - Day 101: 最长公共子序列
数据结构·算法·leetcode·深度优先·动态规划
玖剹21 小时前
二叉树递归题目(一)
c语言·c++·算法·leetcode