Valid Palindrome 解答
Question
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.
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.
Solution
Key to the solution here is to use two pointers. Time complexity O(n), space cost O(1).
public class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() < 2)
return true;
int length = s.length();
int p1 = 0, p2 = length - 1;
char tmp1, tmp2;
while (p1 < p2) {
tmp1 = s.charAt(p1);
while ((!Character.isLetter(tmp1)) && (p1 < p2) && (!Character.isDigit(tmp1))) {
p1++;
tmp1 = s.charAt(p1);
}
if (Character.isLetter(tmp1))
tmp1 = Character.toLowerCase(tmp1);
tmp2 = s.charAt(p2);
while (!Character.isLetter(tmp2) && (p1 < p2) && (!Character.isDigit(tmp2))) {
p2--;
tmp2 = s.charAt(p2);
}
if (Character.isLetter(tmp2))
tmp2 = Character.toLowerCase(tmp2);
if (tmp1 != tmp2)
return false;
p1++;
p2--;
}
return true;
}
}
Valid Palindrome 解答的更多相关文章
- LeetCode: Valid Palindrome 解题报告
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [LeetCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【leetcode】Valid Palindrome
题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- Leetcode Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LintCode] Valid Palindrome 验证回文字符串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- 25. Valid Palindrome
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric char ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Valid Palindrome [LeetCode]
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
随机推荐
- 【HDU1325】Is It A Tree?(并查集基础题)
有以下坑点: 1.结束输入不一定-1,题目中的叙述只是说所有权值都为正值. 2.是否构成一棵树不能只判断是否只有一个根节点,没有环路,而且还需要判断每个节点的入度一定是1,不然就不是一棵树. (无环路 ...
- softlayerFastUploadVHDtoBS
Object Storage Uploader Overview We’ve recently added the option to import customer-supplied Virtual ...
- javascrpit开发连连看记录-小游戏
工作之余,总想做点什么有意思的东西.但是苦于不知道做什么,也就一直没有什么动作.在一个午饭后,跟@jedmeng和@墨尘聊天过程中,发现可以写一些小东西来练练手,有以下几点好处: 1. ...
- shell数组(产生不同的随机数)
#!/bin/bash # declare -a ARRAY read -p "Please input num[1-39]:" EMENUM #对比新生成的随机数是否重复 fun ...
- ASIFormDataRequest 上传图片
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typica ...
- cocos2dx 3.0 研究(4)渲染分析
http://blog.csdn.net/epeaktop/article/details/26730909中已经说明了程序的设计,让我们来看看在cocos2dx 3.0中代码是怎样实现的. void ...
- 【HDU】 1018 Big Number
大意就是求 : log10(n!) = log10(1 * 2 * 3 * .......*n) = log10(1) + log10(2) + ........+log10(n); 打表的话会ML ...
- fedora 20下安装vim的C++补全插件clang_complete
1.安装clang yum install clang 2.安装clang_complete插件 git clone https://github.com/Rip-Rip/clang_complete ...
- RMAN-FORMAT-CONFIGURE及动态性能表
一.FORMAT参数在备份过程中,可指定format参数来自定义备份片段的命令规则,比如: RMAN> BACKUP DATABASE FORMAT 'D:\BACKUP\%U'; RMAN&g ...
- Glide的加载图片的帮助类,用来把图片圆角或者改成圆形图片
Glide虽然非常好用但是没找到把图片圆角的方法,所以百度了一个非常不错的加载类自己实现圆角图 感谢原文章作者:http://blog.csdn.net/weidongjian/article/det ...