leetcode--Valid Palindrome

来源:互联网 发布:sqlserver聚合函数 编辑:程序博客网 时间:2024/05/29 12:19

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.

[java] view plain copy
  1. public class Solution {  
  2.     public boolean isPalindrome(String s) {  
  3.         int low = 0;  
  4.         int high = s.length()-1;  
  5.         while(low<high){  
  6.             if(!check(s.charAt(low))){  
  7.                 low++;continue;  
  8.             }else if(!check(s.charAt(high))){  
  9.                 high--;continue;  
  10.             }else{  
  11.                 if(s.charAt(low)!=s.charAt(high)&&s.charAt(low)-s.charAt(high)!=32  
  12.                         && s.charAt(high)-s.charAt(low)!=32){  
  13.                     return false;  
  14.                 }else{  
  15.                     low++;high--;  
  16.                 }  
  17.             }  
  18.         }  
  19.         return true;  
  20.     }  
  21.       
  22.     public boolean check(char c){  
  23.         if(c>='a'&&c<='z' || c>='A'&&c<='Z' || c>='0'&&c<='9')  
  24.             return true;  
  25.         return false;  
  26.     }  
  27. }  


原文链接http://blog.csdn.net/crazy__chen/article/details/46383581

原创粉丝点击