码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Palindrome

时间:2015-04-09 00:58:05      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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 {
 2     public static boolean isPalindrome(String s)
 3     {
 4         char[]ch=s.toLowerCase().toCharArray();
 5         int i=0,j=ch.length-1;
 6         while(i<j)
 7         {
 8             while(i<ch.length&&!(ch[i]>=‘a‘&&ch[i]<=‘z‘||ch[i]>=‘0‘&&ch[i]<=‘9‘))
 9                 i++;
10             while(j>=0&&!(ch[j]>=‘a‘&&ch[j]<=‘z‘||ch[j]>=‘0‘&&ch[j]<=‘9‘))
11                 j--;
12             if(i>ch.length-1||j<0)
13                 return true;
14             if(ch[i]!=ch[j])
15                 return false;
16             i++;
17             j--;
18             
19         }
20         return true;
21     }
22     public static void main(String args[])
23     {
24         boolean b=isPalindrome(",.");
25         System.out.println(b);
26     }
27 }

 

Valid Palindrome

标签:

原文地址:http://www.cnblogs.com/qq1029579233/p/4404991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!