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" 是一个回文. & ...
随机推荐
- typedeifn typename
1.类型说明typedef 类型说明的格式为: typedef 类型 定义名; 类型说明只定义了一个数据类型的新名字而不是定义一种新的数据类型.定义名表示这个类型的新名字. 例如: 用下面语句定义整 ...
- PS更换证件照颜色
PS是我们经常使用的设计软件,在生活中使用的范围也很广,但是对于普通的用户来说,也就是平时给自己的照片美化一下,还有就是做一些证件照.今天和大家分享的是更改证件照的颜色,网上可能有很多,但是个人感觉都 ...
- 转载.怎样在Quartus II中转化HDL文件为bsf文件?
步骤1 新建或打开Quartus II工程,用QII自带文本编辑器打开HDL文件. 图1 用QII自带的文本编辑器打开HDL文件 步骤2 选择File>Create / Update>Cr ...
- wordpress改不了固定连接的解决办法
经常遇到更改了固定连接刷新没效果>>>>>>>废话不多说直接上步骤,有图有真相. 1,ftp删除根目录的这个文件夹 2,进入wp后台设置-固定连接—自定义结构 ...
- Eclipse自动生成 get/set
步骤一:在声明的数据域中按Ctrl+1: 步骤二:点击最后一个选项Create getter and setter,在弹出的对话框中点击确定: 在介绍另外一个方法: 步骤一:声明完类的数据域之后,输入 ...
- eclipse配置storm1.1.0开发环境并本地跑起来
storm的开发环境搭建比hadoop(参见前文http://www.cnblogs.com/wuxun1997/p/6849878.html)简单,无需安装插件,只需新建一个java项目并配置好li ...
- Java 数据类型及转换
整形: byte(1个字节) 范围:-128~127 short(2个字节) 范围:-215~215-1 (-32768~32767) int(4个字节) 范围:-231~231-1 (-214748 ...
- erlang的tcp服务器模板
改来改去,最后放github了,贴的也累,蛋疼 还有一个tcp批量客户端的,也一起了 大概思路是 混合模式 使用erlang:send_after添加recv的超时处理 send在socket的opt ...
- java写基础的九九乘法表
package com.aaa; public class Xox { public static void main(String[] args) { for (int i = 1; i <= ...
- Java-Maven-Runoob:Maven 项目文档
ylbtech-Java-Maven-Runoob:Maven 项目文档 1.返回顶部 1. Maven 项目文档 本章节我们主要学习如何创建 Maven 项目文档. 比如我们在 C:/MVN 目录下 ...