Valid Palindrome

For example,
"A man,  a plan, a canal: Panama"  is a palindrome.

"race a car" is not a palindrome. java


该题目的有两点须要注意: 正则表达式

1. 大小写转换。 spa

2. 无用字符删除。 code

这两个问题使用java的正则表达式能够轻易解决。 orm

public class Solution {
       public boolean isPalindrome(String s) {
        if(s == null){
            return true;
        }
        
        s = s.toUpperCase().replaceAll("[^0-9a-zA-Z]", "");
        int begin = 0;
        int end = s.length() - 1;
        
        while(begin < end){
        	if(s.charAt(begin++) != s.charAt(end--)){
        		return false;
        	}
        }
        
        return true;
    }
}
相关文章
相关标签/搜索