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 ...
随机推荐
- mook_百度百科
mook_百度百科 mook
- Linux各种包安装及命令
1.Locate yum -y install mlocate 若出现问题: locate: can not stat () `/var/lib/mlocate/mlocate.db': 没有那个文件 ...
- handsontable常规配置的中文API
常规配置: 1.固定行列位置 fixedRowsTop:行数 //固定顶部多少行不能垂直滚动 fixedColumnsLeft:列数 //固定左侧多少列不能水平滚动 2.拖拽行头或列头改变行或列的大小 ...
- as3 updateAfterEvent的作用
flash中一共有三个类具有该属性,这三个类分别是:KeyboardEvent,MouseEvent,TimerEvent.调用updateAfterEvent 属性的事件,可强制立即执行呈现操作,而 ...
- EasyUI 1.3.1以下的组合验证
适用于EasyUI 1.3.1以下的, 1.3.2已经自带组合验证(如validType:['validator1','validator2']) $.extend($.fn.validatebox. ...
- Android Studio稍微较新的版本下载
ALL ANDROID STUDIO PACKAGES-V1.4.1.2422023 Select a specific Android Studio package for your platfor ...
- 关于iOS9之后的loadViewIfNeeded
iOS9之前 有些时候因为需要手动调用loadview 但是有风险,系统不再调用viewDidLoad 所以手动调用loadview是错误的 iOS9之后出现了loadViewIfNeeded解决了这 ...
- 亲试,Windows平台上使用Qt5.2.1编写Android
首先把工具都下载好: 1. Qt for Android: http://qt-project.org/downloads 2. Android NDK http://developer.androi ...
- Lucene学习总结之四:Lucene索引过程分析
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
- js实现页面图片加载进度条
//html <div id="loading" class="loading"> <div class="load"&g ...