Skip to main content

🟢 剑指 Offer II 019. 最多删除一个字符得到回文

LeetCode 提示

题目难度 简单

原题链接 🔗 leetcode

题解1#

class Solution {    private boolean isPalindrome(String s, int lo, int hi) {        while (lo < hi && s.charAt(lo) == s.charAt(hi)) {            lo += 1;            hi -= 1;        }        return lo >= hi;    }
    public boolean validPalindrome(String s) {        int lo=0, hi=s.length()-1;        while (lo < hi && s.charAt(lo) == s.charAt(hi)) {            lo += 1;            hi -= 1;        }
        if (lo >= hi) {            return true;        }
        return isPalindrome(s, lo, hi-1) || isPalindrome(s, lo+1, hi);    }}