[抄题]:

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]:

  1. 空字符串是具体的实例对象,所以用isempty()而不是null
  2. 此题中大小写不敏感,所以需要都转换为小写

[思维问题]:

不知道符号怎么处理:Character.isLetterOrDigit

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 此题特殊:while里面嵌套if else if,一次只操作一位,防止指针没到位就判断对称了.chead ctail属于因变量,都要在循环体内随即变化,下次注意
  2. 一对字母不管是否对称,都要继续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判断有效的有符号的回文串的更多相关文章

  1. 125. Valid Palindrome(判断忽略标点的字符串是否回文,加个正则,与上一题解法一样)

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  2. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  4. Gym - 100570E:Palindrome Query (hash+BIT+二分维护回文串长度)

    题意:给定字符串char[],以及Q个操作,操作有三种: 1:pos,chr:把pos位置的字符改为chr 2:pos:问以pos为中心的回文串长度为多长. 3:pos:问以pos,pos+1为中心的 ...

  5. [LeetCode] Palindrome Partitioning 拆分回文串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  6. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  7. 【一天一道LeetCode】#125. Valid Palindrome

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  8. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

  9. lintcode :Valid Palindrome 有效回文串

    题目: 有效回文串 给定一个字符串,判断其是否为一个回文串.只包含字母和数字,忽略大小写. 样例 "A man, a plan, a canal: Panama" 是一个回文. & ...

随机推荐

  1. 【英语】Bingo口语笔记(87) - 不要做某事的常见表达

  2. xlrd,xlwt读表格、写表格

    转:http://www.jb51.net/article/60510.htm

  3. ORA-12154,TNS错误解决办法

  4. DLatch by Verilog

    //-----------------------------------------------------// Design Name : dlatch_reset// File Name   : ...

  5. tomcat 并发配置优化

    修改tomcat/conf/server.xml配置文件. <Executor name="tomcatThreadPool" namePrefix="catali ...

  6. mysql实战优化之七:数据库侧配置优化

    对于功能,我们可能知道必须改进什么:但对于性能问题,有时我们可能无从下手.其实,任何计算机应用系统最终队可以归结为: cpu消耗 内存使用 对磁盘,网络或其他I/O设备的输入/输出(I/O)操作. 但 ...

  7. Javascript 推荐一个图形化展示库

    觉得这个库不错: http://almende.github.io/chap-links-library/index.html

  8. Py修行路 python基础 (五)三元运算 字符编码 元组 集合 三级菜单优化!

    三元运算 条件判断不能加冒号: a=3 b=5 c=a if a<b else b oct() 转成八进制的简写:16进制 标志:BH为后缀或是0x为前缀hex() 转成16进制 元组 跟列表是 ...

  9. jmeter json截取

    前些日子压测我们系统,发现我们开发把cookie值当成一个参数返回到了json中,这样就要从json中获取这个cookie后配置到cookie 管理器中 Jmeter中本身是不支持直接处理json串的 ...

  10. Python多线程-队列

    队列就是存东西取东西,多用于多线程中 按照顺序 对传入的数据按规定的顺序输出 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" ...