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" 是一个回文. & ...
随机推荐
- HihoCoder 1097 kruscal
最小生成树二·Kruscal算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了——但是幸运的 ...
- SSH实现远程控制
SSH(Secure Shell)是一种能够提供安全远程登录会话的协议,使用ssh可以在远程linux中执行命令. sshd服务提供两种安全验证的方法: (1)基于口令的安全验证:经过验证帐号与密码即 ...
- 转:django在生成数据库时常常遇到的问题
真的很有用! http://blog.csdn.net/pipisorry/article/details/45727309
- 十、python沉淀之路--高阶函数初识
一.高阶函数:分两种:一种是返回值中包含函数体:另一种是把一个函数体当作了参数传给了另一个函数 1.返回值中包含函数体 例1. def test(): print('这是一个测试') return t ...
- 几个ADB常用命令
http://blog.163.com/ymguan@yeah/blog/static/14007287220133149477594/ 1. 显示当前运行的全部模拟器: adb devices ...
- 也谈TDD,以及三层架构、设计模式、ORM……没有免费的午餐,选择了,必付出代价
想在园子里写点东西已经很久了,但一直没有落笔,忙着做 一起帮 的开发直播,还有些软文做推广,还要做奶爸带孩子,还要……好吧,我承认,真正的原因是: 太特么的难写了! 但再难写也要写啊,要等到“能写好了 ...
- maven编译问题:maven编译成功,eclipse文件未编译
我们先来看一个正常的编译流程: 1.从svn上检出一个项目: 2.看该工程是否为maven项目,不是则先转为maven项目:右键单击项目,选择configure->Convert to Mave ...
- ubuntu 环境变量配置
1.vim /etc/environment 2.vim /etc/profile export JAVA_HOME=/opt/jdk1.6.0_45PATH=$JAVA_HOME/bin:/pub/ ...
- 容器中跨主机的网络方案-Weave
容器中的网络是建立docker集群的重要内容. 本文将介绍如何用Weave实现容器的多节点互通. Weave是一个开源的项目,其网站为: https://www.weave.works/ 其工作原理相 ...
- npm国内镜像介绍
这个也是网上搜的,亲自试过,非常好用! 镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set reg ...