680. Valid Palindrome II - Easy

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:

Input: "aba"
Output: True

Example 2:

Input: "abca"
Output: True
Explanation: You could delete the character ‘c‘.

Note:

  1. The string will only contain lowercase characters a-z. The maximum length of the string is 50000.

time: O(n), space: O(1)

class Solution {
    public boolean validPalindrome(String s) {
        int l = 0, r = s.length() - 1;
        while(l <= r) {
            if(s.charAt(l) != s.charAt(r))
                return valid(s, l + 1, r) || valid(s, l, r - 1);
            l++;
            r--;
        }
        return true;
    }
    private boolean valid(String s, int start, int end) {
        int l = start, r = end;
        while(l <= r) {
            if(s.charAt(l) != s.charAt(r))
                return false;
            l++;
            r--;
        }
        return true;
    }
}

原文地址:https://www.cnblogs.com/fatttcat/p/10134974.html

时间: 2024-10-29 03:40:03

680. Valid Palindrome II - Easy的相关文章

680. Valid Palindrome II【easy】

680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You co

【Leetcode_easy】680. Valid Palindrome II

problem 680. Valid Palindrome II 参考 1. Leetcode_easy_680. Valid Palindrome II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/11091358.html

[leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. 思

680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

680. Valid Palindrome II 有效的回文2

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

680. Valid Palindrome II

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Input: "aba" Output: True Input: "abca" Output: True Explanation: You could delete the character 'c'. 判断字符串是不是回文,最多可以删除字符串的一个

125. Valid Palindrome【easy】

125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. No

[LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers

Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation: You could delete the character 'c'. N

164.Valid Palindrome II

题目: Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. 给定非空字符串s,您最多可以删除一个字符. 判断你是否可以成为回文. Example 1: Input: "aba" Output: True Example 2: Input: "abca" Output: True Explanation