原题链接在这里:https://leetcode.com/problems/valid-palindrome-ii/description/

题目:

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 Complexity: O(s.length()). Space: O(1).

AC Java:

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

跟上Valid Palindrome III.

类似Valid Palindrome.

LeetCode Valid Palindrome II的更多相关文章

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

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

    680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...

  3. 680. Valid Palindrome II【easy】

    680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...

  4. 【Leetcode_easy】680. Valid Palindrome II

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

  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]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] Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  8. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  9. [leetcode]Valid Palindrome @ Python

    原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...

随机推荐

  1. PAT 天梯赛 L1-037. A除以B 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...

  2. iOS9 Search API 之 Spotlight

    iOS9以后 有三种api提供搜搜方式 加强引导用户关注 我们的app及相关内容的方式 NSUserActivity Web Markup  Core Spotlight 用法 前两种 实战操作性不够 ...

  3. centos6.8 修改yum安装镜像源

    查看centos系统版本 cat /etc/redhat-release CentOS系统更换软件安装源 第一步:备份你的原镜像文件,以免出错后可以恢复. mv /etc/yum.repos.d/Ce ...

  4. Linux Shell编程 sort、wc命令

    sort命令:字符串排序 sort 命令可以依据不同的数据类型来进行排序.sort 将文件的每一行作为一个单位,相互比较.比较原则是从首字符向后,依次按 ASCII 码值进行比较,最后将它们按升序输出 ...

  5. 【leetcode刷题笔记】Word Ladder II

    Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...

  6. 嵌入式C函数优化

    0. 引言 这是一个简单函数的优化,但却体现了代码易读性和效率的综合考虑. 如果问我如何写出优秀的代码,答曰:再写一版. 1. 版本1 从环形buffer中取出数据,然后放到一个结构体中.buffer ...

  7. 跨平台移动开发_Android 平台使用 PhoneGap 方法

    PhoneGap  下载地址http://phonegap.com/install/    1.打开 Eclipse,在文件菜单下面点击 创建> Android Application Proj ...

  8. jq .attr()和.prop()方法的区别

    今天在nodejs交流群里面遇到别人在里面说面试的时候遇到了这个问题,没回答出来,面试官讲的他也不明白,这个问题看着很简单,但是往深的解释就很难了. 对于HTML元素本身就带有的固有属性,在处理时,使 ...

  9. php数组函数-array_diff()

    array_diff()函数返回两个数组的差集数组.该数组包括了所有在被比较数组中,但是不在任何其他参数数组中的键值在返回数组中,键名保持不变. array_diff(array1,array2,ar ...

  10. MySQL数据库基本操作(三)

    MySQL补充: mysql是关系型数据库,关系数据库,是建立在关系模型基础上的数据库,现实世界中的各种实体,以及实体之间的各种联系,均用关系模型(table)来表示.#关系模型就是指二维表格模型,因 ...