[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'.
Note:
- The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
题目思路就是正常的用two pointers去遍历s, 然后如果有不一样的, 要么把l + = 1, 要么把r -= 1 , 然后如果这两个中有一个是palindrome, 那么就符合.
Code:
class Solution:
def palindrome2(self, s): # len(s) >= 1
l, r = 0, len(s)-1
def helper(l, r, s):
while l < r:
if s[l] == s[r]:
l += 1
r -= 1
else:
return False
return True
while l <r:
if s[l] == s[r]:
l += 1
r -= 1
else:
return helper(l+1, r, s) or helper(l, r-1, s)
return True
[LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers的更多相关文章
- [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有效回文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 (验证回文字符串 Ⅱ)
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(双指针)
题意:给定一个字符串,可以最多去掉一个字符,判断是否可以使该字符串成为一个回文串. 分析:head和tail双指针分别指向字符串首尾,如果s[head] != s[tail],则要么去掉s[head] ...
- 680. Valid Palindrome II【easy】
680. Valid Palindrome II[easy] Given a non-empty string s, you may delete at most one character. Jud ...
- 【Leetcode_easy】680. Valid Palindrome II
problem 680. Valid Palindrome II solution: 不理解判断函数中的节点的问题... class Solution { public: bool validPali ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125. Valid Palindrome
这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...
- [leetcode] 1. Valid Palindrome
leetcode的第一题,回文数判断. 原题如下: For example, "A man, a plan, a canal: Panama" is a palindrome. & ...
随机推荐
- 怎么修改mysql主键(id)的值为自增
alter table tb_name modify id int auto_increment primary key
- Shell 中的反引号(`),单引号('),双引号(")
在写shell的时候老是傻傻分不清楚,今天来理一理. 1.反引号位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引号(')位于Enter键的左方的区别. 在Linux中起着命令替换的作用.命 ...
- Esper学习之八:EPL语法(四)
关于EPL,已经写了三篇了,预估计了一下,除了今天这篇,后面还有5篇左右.大家可别嫌多,官方的文档对EPL的讲解有将近140页,我已经尽量将废话都干掉了,再配合我附上的例子,看我的10篇文章比那140 ...
- ubuntu apt 代理设置
可以通过三种方法为apt-get设置http代理方法一这是一种临时的手段,如果您仅仅是暂时需要通过http代理使用apt-get,您可以使用这种方式.在使用apt-get之前,在终端中输入以下命令(根 ...
- Delete触发器
- sencha touch Carousel 自动切换
代码是在网上找的,忘记原出处了 /** * 跑马灯自动切换 */ Ext.define('ux.RotatingCarousel', { extend: 'Ext.carousel.Carousel' ...
- Protobuf的安装使用
date: 2018-10-12 18:59:13 版权归属原作者,本位转自:https://www.cnblogs.com/autyinjing/p/6495103.html 1. 是什么? Go ...
- 一、laya学习笔记 --- layabox环境搭建 HelloWorld(坑:ts版本问题解决方案)
好吧,使用layabox需要从官网下载些啥呢 一.下载layabox 官网 https://www.layabox.com/ 首页上有两个,一个Engine,一个IDE Engine我下载的TS版本, ...
- C# IEqualityComparer 去重
1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...
- [MySQL]修改root密码的4种方法(以windows为例)
方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = password('新密码'); 例子:my ...