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 ...
随机推荐
- 如何安装/更新ruby,安装cocoapods,为开发做好准备!(2016年12月07日更新内容)
一:首先来说一下如何安装/更新ruby: 一般情况下,即使是新买的Mac电脑也会安装有ruby,可以在终端中键入一下命令查看ruby版本 ruby -v 正常情况下下面会打印出ruby的版本信息,如果 ...
- xml转换为对象 微信接口
public sealed class XMLSerilizable { /// <summary> /// XML转换为对象 /// </summary> /// <t ...
- linux alsa pcm(此pcm非硬件pcm接口)
转:https://blog.csdn.net/crycheng/article/details/7095899 CODEC :音频芯片的控制,比如静音.打开(关闭)ADC(DAC).设置ADC(DA ...
- 根据Django后台的ajax大全
一.什么是ajax 1.1 什么是JSON? AJAX(Asynchronous Javascript And XML)翻译成中文就是“异步Javascript和XML”.即使用Javascript语 ...
- Python3.4 用 pip 安装lxml时出现 “Unable to find vcvarsall.bat ”
我的python版本是Python 3.5 该问题的产生是在windows环境中,python 的 Setup需要调用一个vcvarsall.bat的文件,该文件需要安装c++编程环境才会有.网上的方 ...
- 数据库系统概论学习2-《关系数据库与 E/R 模型》
-----------------------------一直更新学习内容------------------------------------ 建立一个关系数据库需要几步? 2.关系数据库与 E/ ...
- 通过代码或者配置文件 对log4net进行配置
1.通过代码进行配置 1.1代码 http://stackoverflow.com/questions/16336917/can-you-configure-log4net-in-code-inste ...
- java深入探究11-基础加强
1. ? extends String:String 子类;? super String:String 父类 2.反射->参数化类型表示 ParameteredType:参数化类型表示,就是获得 ...
- R语言笔记005——计算描述性统计量
数据的分布特征: 分布的集中趋势,反应各数据向其中心值靠拢或聚集的程度(平均数,中位数,四分位数,众数) 分布的离散程度,反应各数据远离其中心值的趋势(极差,四分位差,方差,标准差,离散系数) 分布的 ...
- MapReduce-从HBase读取数据处理后再写入HBase
MapReduce-从HBase读取处理后再写入HBase 代码如下 package com.hbase.mapreduce; import java.io.IOException; import o ...