LeetCode 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.
Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.
Note:
- 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 (验证回文字符串 Ⅱ)的更多相关文章
- [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 ...
- 【LeetCode】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- Leetcode680.Valid Palindrome II验证回文字符串2
给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 示例 1: 输入: "aba" 输出: True 示例 2: 输入: "abca" 输出: ...
- [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 ...
- [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 ...
- LeetCode OJ:Valid Palindrome(验证回文)
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- 【LeetCode】- Valid Palindrome(右回文)
[ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- LeetCode 680. Valid Palindrome II(双指针)
题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串. 分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head] ...
随机推荐
- Python学习日记之中文支持
解决中文输出错误 在开头添加 # -*- coding: utf-8 -*- 即可
- python利用requests统计1个接口的响应时间
参照 https://www.cnblogs.com/yoyoketang/p/8035428.html requests统计接口的响应时间有2种方式 r.elapsed.total_seconds( ...
- Redis系列(三)--消息队列、排行榜等
Redis命令执行生命周期: 发送命令--->排队(单线程)--->执行命令--->返回结果 慢查询: 只是针对命令执行阶段 慢查询日志通过一个固定长度的FIFO queue,这个q ...
- block相关归纳
经过今天的Block的学习.上网查询相关文章归纳了一下 一.一个使用Block的好处有: Block可以用在许多不同的环境中,这样可以让代码更加简单,以及减少函数声明的数量,不用实现代理协议. 简单性 ...
- 第一章 React新的前端思维方式
---恢复内容开始--- 第一章 React新的前端思维方式 1.1 初始化一个React项目 1.安装create-react-app npm install --global create-rea ...
- 洛谷——P3811 【模板】乘法逆元
P3811 [模板]乘法逆元 线性求逆元 逆元定义:若$a*x\equiv1 (\bmod {b})$,且$a$与$b$互质,那么我们就能定义: $x$为$a$的逆元,记为$a^{-1}$,所以我们也 ...
- Linux 源码
https://elixir.bootlin.com/linux/latest/source
- 集合:Collection
why ? when ? how ? what ? Java 集合框架图 由上图我们可以看到,Java 集合主要分为两类:Collection 和 Map. Collection 接口 遍历 Coll ...
- Django 命令行调用模版渲染
首先我们需要进入 Django 的 shell python manage.py shell 渲染模版中的 name from django.template import Context,Templ ...
- jmeter 性能测试
1. Ramp-up Period(in seconds)代表多长时间内启动所有线程 2. Aggregate Report Samples:总共发给服务器的请求数量 Average:单个请求的平均响 ...