leetcode 125. Valid Palindrome ----- java
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.
判读一个字符串是否是回文字符串(只判断期中的字符和数字)。
1、两个栈,从前向后和从后向前,然后判断两个栈是否相同。
public class Solution {
public boolean isPalindrome(String s) {
Stack stack1 = new Stack<Character>();
Stack stack2 = new Stack<Character>();
s = s.toLowerCase();
char[] word = s.toCharArray();
int len = s.length();
for( int i = 0;i<len;i++){
if( (word[i] >= '0' && word[i] <= '9') || (word[i]>='a' && word[i]<='z') )
stack1.push(word[i]);
if( (word[len-1-i] >= '0' && word[len-1-i] <= '9') || (word[len-1-i]>='a' && word[len-1-i]<='z') )
stack2.push(word[len-1-i]);
}
return stack1.equals(stack2);
}
}
2、直接用两个指针记录left和right即可。
public class Solution {
public boolean isPalindrome(String s) {
char[] word = s.toLowerCase().toCharArray();
int len = s.length();
int left = 0,right = len-1;
while( left < right ){
if( !((word[left] >= '0' && word[left] <= '9') || (word[left]>='a' && word[left]<='z' )) )
left++;
else if( !((word[right] >= '0' && word[right] <= '9') || (word[right]>='a' && word[right]<='z' )) )
right--;
else if( word[left] == word[right]){
left++;
right--;
}else
return false;
}
return true;
}
}
leetcode 125. Valid Palindrome ----- java的更多相关文章
- Java [Leetcode 125]Valid Palindrome
题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- [LeetCode] 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 a ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- LeetCode 125 Valid Palindrome(有效回文)(*)
翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...
- Java for LeetCode 125 Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- Leetcode 125 Valid Palindrome 字符串处理
题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...
- [LeetCode] 680. Valid Palindrome II 验证回文字符串 II
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...
- 125. Valid Palindrome【easy】
125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...
随机推荐
- [转]powerDesigner生成excel版本的数据库文件
powerDesigner生成excel版本的数据库文件 出处:http://ray-allen.iteye.com/blog/1893347 脚本 excel 今天收到一个需求,要把数据库设计给一 ...
- HDU 1465
排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 大家常常 ...
- Visual studio 2013安装及单元测试
vs安装过程 单元测试: 创建c#类库 创建单元测试 测试结果
- matlab函数调用及数据传递
http://www.cnblogs.com/duanp/archive/2008/11/29/Matlab-GUIDE.html函数调用 在一个m文件中,可以定义多个函数,但是文件名一定要与第一个函 ...
- 去除hadoop启动时的警告
hadoop启动的时候,会出现以下警告提示: 执行more start-all.sh查看该文件 但/libexec下不存在hadoop-config.sh文件,所以会执行bin/hadoop-conf ...
- 一道面试题,简单模拟spring ioc
自己实现的,程序写的土了点,很多情况没去考虑,主要是复习理解怎么使用反射来实现spring 的依赖注入. package dom4jtest; import java.lang.reflect.Inv ...
- 国内android帮助文档镜像网站---http://wear.techbrood.com/develop/index.html
http://wear.techbrood.com/develop/index.html
- [zz]论程序员
g9老大多年前的趣文: 论程序员 根据钱钟书先生的<论文人>胡改的.聊搏一笑,文责不负.程序员是可嘉奖的,因为他虚心,知道上进,并不拿身分,并不安本分.真的,程序员对于自己,有时比旁人对于 ...
- HDU 圆桌会议 - 数学题
圆桌 题意就是每分钟可以将相邻的两个人的位置互换一下 , 问你 ,几分钟可以将所有人的位置互换成 原先的 B 在A的右边 C在A的左边 , 换成现在的 C 在A 的右边 , B 在 A 的 ...
- 子线程简单实现(ZT)
4.0以后的android在主线程中不可以直接访问网络,就得用以下的方法来搞,嘿嘿 在主函数中发送一个空的消息 : new Thread(){ @Override public void run() ...