125. Valid Palindrome判断有效的有符号的回文串
[抄题]:
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
用cHead cTail 存头尾字母,不用每次都临时转换
[奇葩corner case]:
- 空字符串是具体的实例对象,所以用isempty()而不是null
- 此题中大小写不敏感,所以需要都转换为小写
[思维问题]:
不知道符号怎么处理:Character.isLetterOrDigit
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 此题特殊:while里面嵌套if else if,一次只操作一位,防止指针没到位就判断对称了.chead ctail属于因变量,都要在循环体内随即变化,下次注意
- 一对字母不管是否对称,都要继续head++ tail-- 促进下一步继续进行,要理解到位
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
while里面嵌套if else if,一次只操作一位,防止指针没到位就判断对称了.chead ctail属于因变量,都要在循环体内随即变化,下次注意
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
public class Solution {
/**
* @param s: A string
* @return: Whether the string is a valid palindrome
*/
public boolean isPalindrome(String s) {
//cc
if (s.isEmpty()) {
return true;
}
//define
int head = 0, tail = s.length() - 1;
while (head < tail) {
char cHead = s.charAt(head), cTail = s.charAt(tail);
//judge:punction or not
if (!Character.isLetterOrDigit(cHead)) {
head++;
}else if (!Character.isLetterOrDigit(cTail)) {
tail--;
}else {
//tolowercase and judge
if (Character.toLowerCase(cHead) != Character.toLowerCase(cTail)) {
return false;
}
//continue
head++;
tail--;
}
}
//return
return true;
}
}
125. Valid Palindrome判断有效的有符号的回文串的更多相关文章
- 125. 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 alphanumeric characters and ignori ...
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)
题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...
- [LeetCode] Palindrome Partitioning 拆分回文串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- 【一天一道LeetCode】#125. Valid Palindrome
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
- lintcode :Valid Palindrome 有效回文串
题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...
随机推荐
- CVS winCVS配置讲解及用户管理
首先 用到工具包包括 CVSNT ,WinCvs及相关python 2.2.3和TCL832 可点此链接下载 http://files.cnblogs.com/lppblogs/%E6%96%87%E ...
- tensorflow中常用激活函数和损失函数
激活函数 各激活函数曲线对比 常用激活函数: tf.sigmoid() tf.tanh() tf.nn.relu() tf.nn.softplus() tf.nn.softmax() tf.nn.dr ...
- 关于Bootstrap table的回调onLoadSuccess()和onPostBody()使用小结
关于Bootstrap table的回调onLoadSuccess()和onPostBody()使用小结 Bootstrap table 是一款基于 Bootstrap 的 jQuery 表格插件, ...
- python函数返回值
2016-08-09 15:01:38 python函数返回值使用return语句,可以返回任意类型的数.如果return语句执行,它之后的所有语句都不再执行. def func(x,y): pri ...
- Django的CSRF机制
原文链接:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 必须有的是: 1.每次初始化一个项目时,都能看到django.mi ...
- 十一、python沉淀之路--map函数、filter函数、reduce函数、匿名函数、内置函数
一.map函数 1.自定义函数,实现类似于map函数的功能 num_l = [1,3,4,5,6,9] def power(n): return n ** 2 def map_test(func,ar ...
- miniofs 配置使用
1. rpm // RPM 包下载 https://github.com/minio/minfs/releases/tag/RELEASE.2017-02-26T20-20-56Z // 安装 yu ...
- Android Studio 打包及引用 AAR(可能是史上最详细的 )
https://www.jianshu.com/p/1777a634db5e Android Library(AAR) 的好处 Android 库在结构上与 Android 应用模块相同.它可以提供构 ...
- Swift app中的Crash捕获与处理
1. 为什么会Crash 常见的Crash原因有:访问已经被释放的内存,数组越界,使用!解包值为nil的变量.当遇到这些情况时,说明应用已经遇到了很严重的非预期错误,无法再继续运行.操作系统检测到这些 ...
- JAX-RS之queryparam、PathParam、DefaultValue、FormParam、Context、RestController等
这几天做东西接触了JAX-RS的东西,没有系统的从开始就学,只是单纯去复制粘贴的用,主要用到了几个Annotations变量,具体如下: queryparam.PathParam.FormParam. ...