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.

题目标签:String

  题目给了我们一个String s,让我们判断它是否是palindrome,允许我们删除一个char。

  利用two pointers,left 和 right 从两边开始,问题在于,当遇到两个 char 不同的时候,我们需要分别删除left 和 right char 去判断它是不是 panlindrome,因为两个选择都有可能是palindrome。

  具体看code。

Java Solution:

Runtime beats 98.73%

完成日期:04/20/2018

关键词:two pointers

关键点:用 || 来 测试两个选择性

 class Solution
{
public boolean validPalindrome(String s)
{
char [] arr = s.toCharArray();
int left = 0;
int right = arr.length - 1; while(left < right)
{
if(arr[left] != arr[right])
{
return isPalindrome(arr, left + 1, right) || isPalindrome(arr, left, right - 1);
} left++;
right--;
} return true;
} public boolean isPalindrome(char[] arr, int left, int right)
{
while(left < right)
{
if(arr[left] != arr[right])
return false; left++;
right--;
} return true;
}
}

参考资料:https://leetcode.com/problems/valid-palindrome-ii/discuss/107716/Java-O(n)-Time-O(1)-Space

LeetCode 题目列表 - LeetCode Questions List

题目来源:https://leetcode.com/

LeetCode 680. Valid Palindrome II (验证回文字符串 Ⅱ)的更多相关文章

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

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

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

  3. Leetcode680.Valid Palindrome II验证回文字符串2

    给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...

  4. [LeetCode] Valid Palindrome II 验证回文字符串之二

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

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

  6. LeetCode OJ:Valid Palindrome(验证回文)

    Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...

  7. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

  8. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  9. LeetCode 680. Valid Palindrome II(双指针)

    题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串. 分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head] ...

随机推荐

  1. 北大ACM(POJ1018-Communication System)

    Question:http://poj.org/problem?id=1018 问题点:枚举. Memory: 564K Time: 329MS Language: C++ Result: Accep ...

  2. 【DVWA】【SQL Injection(Blind)】SQL盲注 Low Medium High Impossible

    1.初级篇 Low.php 加单引号提交 http://localhost/DVWA-master/vulnerabilities/sqli_blind/?id=1'&Submit=Submi ...

  3. Container Views

    https://developer.apple.com/documentation/uikit/views_and_controls Container Views Organize and pres ...

  4. Call stack Structure

                      The stack frame at the top of the stack is for the currently executing routine. Th ...

  5. 【maven】Description Resource Path Location Type An error occurred while filtering resources TESTVIDEO line

    在maven中构建项目的时候发现了如下错误: Description Resource Path Location Type An error occurred while filtering res ...

  6. TWaver推智能手表挑战华为苹果

    2015年的春节刚过,苹果.华为.三星就紧锣密鼓的发布了各自新产品.华为.苹果的智能手表最吸引眼球.TWaver也不甘示弱,立刻连夜推出了更像传统奢侈豪华手表的TWaver Watch,予以反击.看来 ...

  7. struts2源码下载链接

    http://blog.csdn.net/qq_qun_247286682/article/details/6975298

  8. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  9. * average vector from multiple files--Matlab

    n=359;a=[];b=[];c=[];% for loopfor i=1:n      filename=sprintf('output_%d.dat',i);    fileinfo = imp ...

  10. @Value取值为NULL的解决方案------https://blog.csdn.net/zzmlake/article/details/54946346

    @Value取值为NULL的解决方案 https://blog.csdn.net/zzmlake/article/details/54946346