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.

错误解法:

 class Solution {
public:
bool validPalindrome(string s) {
int start = ;
int end = s.length() - ;
int flag = false; while (start <= end) {
if (s[start] == s[end]) {
++start;
--end;
}
else {
if (!flag) {
flag = true; if (s[start + ] == s[end]) {
++start;
} else if (s[start] == s[end - ]) {
--end;
} else {
return false;
}
}
else {
return false;
}
}
} return true; }
};

没有通过以下测试用例:

"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"

考察代码逻辑,发现一开始我们命中了

s[start + 1] == s[end]

这个逻辑,"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga",并且设置了标志位,这就坑了,下次发现不相等的情况,就直接返回false了。

其实我们本意是要命中下面的逻辑

s[start] == s[end - 1]

"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga",并且设置了标志位,这样就可以满足测试用例。

就是说每次我们用

s[start + 1] == s[end]

s[start] == s[end - 1]

其实是一种“或”的关系,所以我们要一判断就判断到底,比如下面的解法一。

解法一:

 class Solution {
public boolean validPalindrome(String s)
{
int left = , right = s.length() - ; while (left < right)
{
if (s.charAt(left) == s.charAt(right))
{
left++; right--;
}
else
{
//remove right
int templeft = left, tempright = right - ; while (templeft < tempright)
{
if (s.charAt(templeft) != s.charAt(tempright)) break;
templeft++; tempright--; if (templeft >= tempright) return true;
} //remove left
left++; while (left < right)
{
if (s.charAt(left) != s.charAt(right)) return false;
left++; right--;
}
}
}
return true;
}
}

参考@yashar 的代码。

解法二:

 class Solution {
public boolean validPalindrome(String s)
{
return validPalindrome(s, , s.length() - , false);
} private boolean validPalindrome(String s, int left, int right, Boolean mismatch)
{
if (right < left) return true; if (s.charAt(left) != s.charAt(right))
{
if (mismatch == true)
return false; return validPalindrome(s, left, right - , true) || validPalindrome(s, left + , right, true);
}
else
{
return validPalindrome(s, left + , right - , mismatch);
}
}
}

递归,参考@yashar 的代码。

解法三:

 class Solution {
public:
bool validPalindrome(string s) {
return valid(s, , s.length() - , );
} private:
bool valid(string& s, int i, int j, int d) { // d: num of chars you can delete at most
if (i >= j) return true;
if (s[i] == s[j])
return valid(s, i + , j - , d);
else
return d > && (valid(s, i + , j, d - ) || valid(s, i, j - , d - ));
}
};

递归,参考@alexander 的代码。

680. Valid Palindrome II【easy】的更多相关文章

  1. 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 pa ...

  2. 【Leetcode_easy】680. Valid Palindrome II

    problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...

  3. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  4. 219. Contains Duplicate II【easy】

    219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...

  5. 【LeetCode】680. Valid Palindrome II

    Difficulty:easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...

  6. [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 pa ...

  7. [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 pa ...

  8. 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...

  9. 491. Palindrome Number【easy】

    Check a positive number is a palindrome or not. A palindrome number is that if you reverse the whole ...

随机推荐

  1. 【2-SAT】【并查集】ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) D. The Door Problem

    再来回顾一下2-SAT,把每个点拆点为是和非两个点,如果a能一定推出非b,则a->非b,其他情况同理. 然后跑强连通分量分解,保证a和非a不在同一个分量里面. 这题由于你建完图发现都是双向边,所 ...

  2. Scala零基础教学【1-20】

    基于王家林老师的Spark教程——共计111讲的<Scala零基础教学> 计划在9月24日内完成(中秋节假期之内) 目前18号初步学习到25讲,平均每天大约完成15讲,望各位监督. 初步计 ...

  3. Android闪闪发光字体效果

    原文: http://blog.csdn.net/xu_fu/article/details/24484019 import android.content.Context; import andro ...

  4. JNI之数组

    Array Operations -- 数组操作 1.GetArrayLength jsize GetArrayLength(JNIEnv *env, jarray array); Returns t ...

  5. MathType如何插入竖直线

    不用键盘上的竖线,用左竖直线和右竖直线.

  6. Getting terminal width in C?

    转:http://stackoverflow.com/questions/1022957/getting-terminal-width-in-c 方法一: #include <sys/ioctl ...

  7. DELPHI HMAC256

    DELPHI HMAC256   unit HMAC;interfaceuses  System.SysUtils,  EncdDecd,  IdHMAC,  IdSSLOpenSSL,  IdHas ...

  8. Android修改状态栏颜色全方位教程

    关键字:状态栏着色 透明状态栏 沉浸式 白底黑字 Github Demo:https://github.com/imflyn/Eyes 参考文章: Android-transulcent-status ...

  9. iOS: iOS9 beta 请求出现App Transport Security has blocked a cleartext HTTP (http://)

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  10. [转载]使用32位64位交叉编码混淆来打败静态和动态分析工具 - wildsator

    0×00 摘要 混淆是一种能增加二进制分析和逆向工程难度与成本的常用技术.主流的混淆技术都是着眼于使用与目标CPU相同的机器代码,在相同的处理器模式下,隐藏代码并进行控制.本文中引入了一种新的混淆方法 ...