leetcode:Valid Palindrome

来源:互联网 发布:区域网络管理 编辑:程序博客网 时间:2024/06/14 14:02

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.

public class Solution {    public boolean isPalindrome(String s) {        if(s == null)            return true;        s = s.toLowerCase();        char[] ch = new char[s.length()];        int m = 0;        for(int i = 0;i< s.length();i++){            if((s.charAt(i) >= '0' && s.charAt(i) <= '9') || (s.charAt(i) >= 'a' && s.charAt(i) <= 'z')){                ch[m] = s.charAt(i);                m++;            }        }                int j = 0;        for(int i = ch.length-1;i >=0;i--){              if(!((ch[i] >= '0' && ch[i] <= '9')||(ch[i] >= 'a' && ch[i] <= 'z')))                  continue;               else{                  if(ch[i] != ch[j])                      return false;                  j++;              }        }        return true;    }}


0 0