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

leecode 回文字符串加强版

时间:2014-07-03 19:38:58      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   os   for   

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 boolean isPalindrome(String s) {
 3          
 4         int low=0;
 5         int high=s.length()-1;
 6         String s1=s.toLowerCase();
 7         while(low<high)
 8         {
 9           
10             while(low<high)
11             {
12                char c=s1.charAt(low);
13                if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))
14                {
15                    break;
16                }
17                else  low++;
18                 
19             }
20             
21              while(low<high)
22             {
23                char c=s1.charAt(high);
24                if((c>=‘a‘&&c<=‘z‘)||(c>=‘0‘&&c<=‘9‘))
25                {
26                    break;
27                }else high--;
28                 
29             }
30             
31             
32             
33             if(s1.charAt(low)==s1.charAt(high)) 
34             {
35                 low++;
36                 high--;
37             }
38             else
39             {
40                 return false;
41             }
42            
43             
44             
45         }
46         
47         return true;
48         
49         
50     }
51 }

 

leecode 回文字符串加强版,布布扣,bubuko.com

leecode 回文字符串加强版

标签:style   blog   color   strong   os   for   

原文地址:http://www.cnblogs.com/hansongjiang/p/3821322.html

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