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 ...
随机推荐
- Sum Root to Leaf Numbers 解答
Question Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
- SOCKET 实现NAT 穿越
在当前IPv4NAT盛行的网络环境下,两个用户要直接进行P2P连接是非常困难的.较好的解决办法是借助含公网的用户或是服务器中介实现P2P连接. NAT:Network Address Translat ...
- Unity 异步加载场景
效果图如下: 今天一直在纠结如何加载场景,中间有加载画面和加载完毕的效果动画! A 场景到 B , 看见网上的做法都是 A –> C –> B. C场景主要用于异步加载B 和 播放一些 ...
- linux cache swap 以及 虚拟内存等
提出四个问题及解答: 1)若进程在运行过程中,物理内存不足会发生什么? 2)为何进程在占用物理内存不变的情况下,系统的物理内存会增加? 3)为何程序的大小大于实际占用的物理内存?(假如程序30M,却只 ...
- 飘逸的python - hack输出流便于调试
当项目有很多文件时,要找出控制台的输出是在哪里print出来的很麻烦,不过这事对于强大的python来说小菜一碟. 先上代码和效果,再说明. import sys,traceback class my ...
- oracle开启/关闭归档模式
1.改变非归档模式到归档模式: 1)SQL> conn / as sysdba (以DBA身份连接数据库) 2)SQL> shutdown immediate;(立即关闭数据库) 3)SQ ...
- html中把li前面的的小圆点换成小图片的方法
li { list-style: none; background: url(../img/li_dis.png) no-repeat left; padding-left: 20px; }
- 《JavaScript 闯关记》之垃圾回收和内存管理
JavaScript 具有自动垃圾收集机制(GC:Garbage Collecation),也就是说,执行环境会负责管理代码执行过程中使用的内存.而在 C 和 C++ 之类的语言中,开发人员的一项基本 ...
- 失效的URL访问限制(转)
* 经常URL的保护仅仅是连接到该页面的链接不出现在未授权的用户面前.然而,一个有动机的.熟练的或者仅仅是幸运的黑客可能会找到并访问这些网页 , 调用这些功能并查看数据.在应用程序中,通过隐匿来实现安 ...
- javascript运动功能-分享到
<script> //窗体载入,为div控件绑定事件 window.onload = function () { var div1 = document.getElementById('d ...