[leedcode 125] Valid Palindrome
2015-07-24 20:35
113 查看
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
For example,
"A man, a plan, a canal: Panama"is a palindrome.
"race a car"is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
public class Solution { //注意题目不考虑大小写,因此需要利用toLowerCase进行转换,定义一个函数判断字符是否是数值或字符,从头到尾开始向中间遍历 public boolean isPalindrome(String s) { if(s==null||s.length()<=0) return true; s=s.toLowerCase(); int left=0; int right=s.length()-1; while(left<right){ while(left<right&&!isValid(s.charAt(left))){ left++; } while(left<right&&!isValid(s.charAt(right))){ right--; } if(left<right&&s.charAt(left)!=s.charAt(right))return false; left++; right--; } return true; } public boolean isValid(char a){ if(a>='a'&&a<='z'||a>='0'&&a<='9')return true; return false; } }
相关文章推荐
- C++每次读取一行字符串输入(学习笔记) (转)
- java常量使用
- UVALive 5088 Alice and Bob's Trip(树形DP)
- Linux Fedora 22 配置IP地址文件
- oc014---oc的数据类型
- hdu5246 贪心
- 一切成功源于积累——20150723 货币对之澳美 典型跌势 5分钟定理 八连斩 再次中计
- Top 10 Algorithms of 20th and 21st Century
- 【剑指offer】数值的整数次方
- 路径里的正斜杠和反斜杠
- 图像标注 图像分割,特征提取和分类算法
- Linux Kernel系列 - 黄牛X内核代码凝视
- phalcon安装笔记
- 史上最强劲的android模拟器命令详解
- 学习集合Collection , Map
- mysql部分总结
- 堆和栈的区别(转过无数次的文章)
- 命令执行顺序控制与管道
- hdu1568
- python---mongodb