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的更多相关文章

  1. Java [Leetcode 125]Valid Palindrome

    题目描述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  2. [LeetCode] 125. Valid Palindrome 验证回文字符串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. LeetCode 125. Valid Palindrome

    这个题目只要注意大小写问题即可解决,而且我们只关注的是字母和数值 Given a string, determine if it is a palindrome, considering only a ...

  4. [leetcode]125. Valid Palindrome判断回文串

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  5. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  6. Java for LeetCode 125 Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. Leetcode 125 Valid Palindrome 字符串处理

    题意:判断字符串是否是回文字符串 先将所有的字母和数字字符保留,并将大写字母转化成小写字母,然后将字符串倒置,比较前后两个字符串是否相同. 该题最好的解法可以模仿 Leetcode 345 Rever ...

  8. [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 ...

  9. 125. Valid Palindrome【easy】

    125. Valid Palindrome[easy] Given a string, determine if it is a palindrome, considering only alphan ...

随机推荐

  1. [开发笔记]-VS2012打开解决方案崩溃或点击项目崩溃

    下午在使用VS2012建立Service服务项目时,只要一切换到设计视图页面中,VS就崩溃重启,从网上找了一种方法来解决,测试可行.但导致该问题的原因未知. 解决方案: 步骤1:开始-->所有程 ...

  2. 戴文的Linux内核专题:01介绍

    转自Linux中国 译者按:本文作者戴文.科利尔.约翰逊(Devyn Collier Johnson)今年才19岁,但是他在Linux内核.人工智能.编程语言方面拥有丰富的经验,本文是其在linux. ...

  3. 为什么要进行傅立叶变换?傅立叶变换究竟有何意义?如何用Matlab实现快速傅立叶变换

    写在最前面:本文是我阅读了多篇相关文章后对它们进行分析重组整合而得,绝大部分内容非我所原创.在此向多位原创作者致敬!!!一.傅立叶变换的由来关于傅立叶变换,无论是书本还是在网上可以很容易找到关于傅立叶 ...

  4. IOS弹出视图 LewPopupViewController

    LewPopupViewController是一款IOS弹出视图软件.iOS 下的弹出视图.支持iPhone/iPad. 软件截图 使用方法 弹出视图 1 2 3 4 5 PopupView *vie ...

  5. 《day11---内部类&匿名内部类》

    //79-80-面向对象-内部类-体现 /* 当A类中的内容要被B类直接访问,而A类还需要去创建B类的对象,访问B的内容时, 这时可以将B类定义到A类的内部,这样访问更为便捷. 将B类称之为内部类(内 ...

  6. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

  7. 实现简单ThreadPool

    在很多的应用场景,需要根据任务去创建线程去异步处理问题,不过不停的创建线程和销毁线程本身是一个非常耗时和消耗系统资源的事情,所以通常这种情况使用线程池来实现,常用的场景比如web容器对于web请求的处 ...

  8. Ubuntu下安装lamp

    在Ubuntu里安装PHP环境时可以用恨简单的方法,直接用tasksel命令安装. 首先要安装这个命令: sudo apt-get install tasksel 然后,sudo tasksel in ...

  9. vs2013 c++代码内出现中文导致编译错误

    简单的做法就是,首先,菜单栏->文件->高级保存选项,选择utf-8 无签名, 然后,如果是发现注释语句里有中文,可以让注释语句与下行代码中间空一行, 如果是代码里有用到中文,那么就在中文 ...

  10. TC Hash Filter

    Overview The u32 filter allows you to match on any bit field within a packet, so it is in some ways ...