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 ...
随机推荐
- Friendly ARM linux交叉编译问题解决
ARM-LINUX-GCC 安装参考:(笔记)Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境) 然而安装完成之后运行 arm-linux-gcc -v (注意g ...
- MpVue开发之swiper的使用
用到的关键字如下: class :class current :current bindchange @change circular 是否实现无限滑动 true/false skip-hidden ...
- shfileoperation 删除文件 FileDelete(CString strName)
From:http://blog.csdn.net/lvwx369/article/details/41440883 注意:其中namePath 为全局变量 Cstring namePath; BOO ...
- shell 中并发执行
http://bbs.51cto.com/thread-1104907-1-1.html http://www.51testing.com/html/28/116228-238978.html htt ...
- [leetcode]_Interleaving String
下午去蹭了一发新浪的笔试. 炒鸡多的网络基础知识,总共18道题,就写了8道左右吧,剩下的全是网络知识,这部分抽时间至少过一过. 其中一道算法题,回来跟嘟嘟商量,才发现是leetcode上的原题,连ex ...
- linux之使用samba实现文件共享
早期网络想要在不同主机之间共享文件大多要用FTP协议来传输,但FTP协议仅能做到传输文件却不能直接修改对方主机的资料数据,这样确实不太方便,于是便出现了NFS开源文件共享程序,NFS是一个能够将多台L ...
- MYSQL 级联 添加外键
MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作 ...
- 微软白板Excel xls列号数字转字母
Excel xls列号数字转字母 https://blog.csdn.net/lf124/article/details/53432817?utm_source=itdadao&utm_med ...
- Weex 解析(二)—— NativeBridge
(本篇幅主要讲解Weex 中iOS native与js交互实现) 大纲: weex 总框架预览 iOS NativeBridge总设计原理 一.weex 总框架预览 在写NativeBridge 总设 ...
- avalon 总线时序关系理解
对于读,等待时间指的是从端口捕获数据的时间相对于read信号的延时 建立时间指的是read信号相对于chipselect和addr的延时时间 对于写,等待时间指的是相对于非等待情况下各个信号的延时时间 ...