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'.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
忘了数据结构吧,基本就是指针的问题
ispalindrome是封装的基础方法
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 抓关键字:最多移动一位,后面的都必须符合,所以基础判断也要用while
- 默认情况一般是返回true
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
抓关键字:最多移动一位,后面的都必须符合,所以基础判断也要用while
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
ispaindrome函数里用了while:因为最多只允许一位发生变化
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/**
* @param s: a string
* @return: nothing
*/
public boolean validPalindrome(String s) {
//cc
if (s.length() == 0) {
return true;
}
int l = 0, 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);
}
}
return true;
}
public boolean isPalindrome(String s, int left, int right) {
while ((left++) < (right--))
if (s.charAt(left) != s.charAt(right)) return false;
return true;
}
}
680. Valid Palindrome II 对称字符串-可删字母版本的更多相关文章
- 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】680. Valid Palindrome II 验证回文字符串 Ⅱ(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 思路来源 初版方案 进阶方案 日期 题目地址 ...
- 680. Valid Palindrome II【Easy】【双指针-可以删除一个字符,判断是否能构成回文字符串】
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 ...
- Python 解LeetCode:680. Valid Palindrome II
题目:给定一个字符串,在最多删除一个字符的情况下,判断这个字符串是不是回文字符串. 思路:回文字符串,第一想到的就是使用两个指针,前后各一个,当遇到前后字符不一致的时候,有两种情况,删除前面字符或者删 ...
- 【LeetCode】680. Valid Palindrome II
Difficulty:easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/valid-palindrome ...
随机推荐
- (十八)js控制台方法
console.log 以日志的形式打印 console.warn 输出警示信息 console.info 输出提示信息 console.error 输出错误信息 console.debug 输出调试 ...
- Android UI之LinearLayout详解
※※※摘自http://www.cnblogs.com/salam/archive/2010/10/20/1856793.html LinearLayout是线性布局控件,它包含的子控件将以横向或竖向 ...
- 51nod 2006 飞行员配对
第二次世界大战时期,英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2名飞行员,其中1名是英国飞行员,另1名是外籍飞行员.在众多的飞行员中, ...
- jfrog artifactory jenkins pipeline 集成
1. 预备环境 artifactory ( 开源版本 ) maven jenkins jenkins artifactory plugin (在插件管理安装即可) 2. 配置artifactory ...
- 【Swift】 - 函数(Functions)总结 - 比较 与 C# 的异同
1.0 函数的定义与调用( Defining and Calling Functions ) 习惯了C#了语法,看到下面的这样定义输入参数实在感到非常别扭,func 有点 Javascript的感觉, ...
- bzoj 4104 [Thu Summer Camp 2015]解密运算——思路
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4104 想了很久,想出一个 nlogn (也许是 n2logn )的,可惜空间是 n2 . 已 ...
- dataView 工具栏
option = { tooltip : { trigger: 'axis' }, legend: { data:['最高','最低'] }, toolbox: { show : true, orie ...
- FPGA各大厂商,不可不知
引言: FPGA市场前景诱人,但是门槛之高在芯片行业里无出其右.全球有60多家公司先后斥资数十亿美元,前赴后继地尝试登顶FPGA高地,其中不乏英特尔.IBM.德州仪器.摩托罗拉.飞利浦.东芝.三星这样 ...
- linux文件系统命令和分区 挂载
文件系统命令df [选项][挂载点]选项:-a 显示所有的文件系统信息,包括特殊文件,如/proc,/sysfs-h 使用习惯单位显示容量,如KB,MB或GB等-T 显示文件系统类型-m 以MB为单位 ...
- php设计模式之单例(多例),注册器,观察者模式
单例(Singleton)模式和不常见的多例(Multiton)模式控制着应用程序中类的数量.如模式名称,单例只能实例化一次,只有一个对象,多例模式可以多次实例化. 基于Singleton的特性,我们 ...