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‘.

判断字符串是不是回文,最多可以删除字符串的一个元素来使它成为回文。

解决:从字符串两端开始比较,用一个指针low从前遍历,用high从尾部遍历。

1、当s[low] == s[high],说明符合回文,++low, --high,遍历下一组。

2、要是s[low] != s[high],就要删除一个数,重组。重组的原则就是s[low]和s[high-1]比较,s[high]和s[low+1]比较,是否能配对。

2.1、要是s[low+1] == s[high],那就删除当前的s[low];对应的s[low] == s[high-1],就删除当前的s[high]。

2.2、要是两个都符合条件,就要顺次看下一对能不能组合。

2.3、要是两个都不符合条件,那就说明只删除一个元素不能形成回文。

class Solution {
public:
    bool validPalindrome(string s) {
        int low = 0;
        int high = s.length() - 1;
        int cnt = 0;
        while (low<high) {
            if (s[low] != s[high]) {
                if (s[low+1] == s[high] && s[low] == s[high-1]) {
                    if (s[low+1] == s[high] && s[low+2] == s[high-1])
                        ++low;
                    else if (s[low] == s[high-1] && s[low+1] == s[high-2])
                        --high;

                }
                else if (s[low+1] == s[high])
                    ++low;
                else if (s[low] == s[high-1])
                    --high;
                else
                    return false;
                ++cnt;
            }
            if (cnt==2)
                return false;
            ++low;
            --high;
        }
        return true;
    }
};

原文地址:https://www.cnblogs.com/Zzz-y/p/8322046.html

时间: 2024-10-14 09:36:55

680. Valid Palindrome II的相关文章

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【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

[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

*LeetCode--Valid Palindrome II

Valid Palindrome 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