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. Noip2014 提高组 T2 联合权值 连通图+技巧

    联合权值 描述 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 WiWi, 每条边的长度均为 1.图上两点(u, v)的距离定义为 u 点到 v 点的 ...

  2. Python 条件判断 循环

    age = 20 if age >= 18: print('your age is', age) print('adult') 根据Python的缩进规则,如果if语句判断是True,就把缩进的 ...

  3. C# Process打开程序并移动窗口到指定位置

    process.start只是按指定的参数来运行一个程序,而这个程序自己运行起来是什么样子的就不是Process所能处理的了,不过当程序运行起来后倒是可以通过Process的MainWindowHan ...

  4. C语言实例代码

    绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...

  5. SharePoint 2013 开发——APP安全模型

    博客地址:http://blog.csdn.net/FoxDave 除非开启了SharePoint网站的匿名访问,否则对于入站的请求,必须要有一个身份验证的过程(Authentication),这个 ...

  6. Event Handling on Mac

    Keyboard/Mouse Event + Cocoa AppleEvent + Cocoa AppleEvent + CommandLine App(w/o UI) + CoreFoundatio ...

  7. (转)iphone数据存储之-- Core Data的使用

    原文:http://www.cnblogs.com/xiaodao/archive/2012/10/08/2715477.html iphone数据存储之-- Core Data的使用(一)   一. ...

  8. 偶遇makeblock

    上周出去吃饭在公交站牌上看到一家叫“创客工场”的公司在打招聘广告,当时在想这公司有钱啊,广告都打到这儿了,也没细想让韩总扫了一下他们的二维码,回来搜了一下这个公司,我靠,是做开源硬件的:再一看各种机器 ...

  9. 爬虫学习----pattern

    1.match match(string[, pos[, endpos]]) | re.match(pattern, string[, flags]): 这个方法将从string的pos下标处起尝试匹 ...

  10. Palindrome Number ---- LeetCode 009

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...