LeetCode Valid Palindrome II
原题链接在这里: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:
- 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;
}
}
LeetCode Valid Palindrome II的更多相关文章
- [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) 1
680. 验证回文字符串 Ⅱ 680. Valid Palindrome II 题目描述 给定一个非空字符串 s,最多删除一个字符.判断是否能成为回文字符串. 每日一算法2019/5/4Day 1Le ...
- 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] 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] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode——Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [leetcode]Valid Palindrome @ Python
原题地址:https://oj.leetcode.com/problems/valid-palindrome/ 题意: Given a string, determine if it is a pal ...
随机推荐
- UI控件之UIView与动画
UIView:用来展示用户的界面,响应用户的操作(继承自UIResponder) UIView的作用:绘图.动画.处理事件 UIView可以包含和管理子视图,并且决定子视图的位置大小 获取所有的子视图 ...
- InnoDB存储引擎内存缓冲池管理技术——LRU List、Free List、Flush List
InnoDB是事务安全的MySQL存储引擎,野山谷OLTP应用中核心表的首选存储引擎.他是基于表的存储引擎,而不是基于数据库的.其特点是行锁设计.支持MVCC.支持外键.提供一致性非锁定读,同时被设计 ...
- Linux基本命令 网络命令
概述 网络和监控命令类似于这些: hostname, ping, ifconfig, iwconfig, netstat, nslookup, traceroute, finger, telnet, ...
- PHP类的变量与成员,及其继承、访问与重写要注意的问题
PHP的类及其实例: <?php ?> 后期静态绑定:为了避免子类重写静态属性后,使用继承来的方法仍然方法父类的静态属性,PHP5.3增加了一个新的语法,后期静态绑定,使用static关 ...
- INSPIRED启示录 读书笔记 - 第27章 合理运用瀑布式开发方法
瀑布式开发方法的基本原则 1.采用阶段式开发:软件开发过程被事先分成固定的几个阶段,撰写书面的需求说明文档.设计高层软件架构.设计低层细节.编写代码.测试.部署 2.采用阶段式评审:每个阶段结束后,对 ...
- 1.linux源码安装nginx
从官网下载nginx.tar.gz源码包 拷贝至Linux系统下进行解压 tar -zxvf nginx.tar.gz 进入解压后的目录,需要./configure,此步骤会报多个错,比如没有安装gc ...
- python实现免密码登录lunx服务器
import paramikoimport oshostname='192.168.76.10'username='root'# password='123456'ssh=paramiko.SSHCl ...
- ADO.Net连接Mysql
首先下载一个mysql.data.dll拷贝到bin下面并引用一下 using MySql.Data.MySqlClient; class Program { static void Main(str ...
- cuffdiff problem--Performed 0 isoform-level transcription difference tests
如何解决cuffdiff运行结果中表达量为0的情况? cuffdiff -o cdiffout -b ref.fasta -u -p 15 --library-type fr-firststrand ...
- 【codevs1069】关押罪犯[noip2010](并查集)
题目描述 Description S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极 不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨 ...