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;
    }
};
相关推荐
Pniubi9 小时前
力扣54.螺旋矩阵
算法·leetcode·矩阵
Rain的Java大神之路20 小时前
LeetCode 37 解数独面试攻略:回溯 + 位掩码 + MRV,从暴力到毫秒级求解
java·后端·leetcode
Pniubi21 小时前
力扣73 矩阵置零
算法·leetcode·矩阵
文丑颜不良啊2 天前
LeetCode2261~2280题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊2 天前
LeetCode2321~2340题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z3 天前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
文丑颜不良啊3 天前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊3 天前
LeetCode2141~2160题目解析
java·leetcode