Valid Palindrome

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.

很简单的一个回文判断,只是要忽略大小写以及非字母和数字的char

public class Solution {
    public boolean isPalindrome(String s) {
        s = s.trim().toLowerCase();<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">忽略大小写</span>
        int low = 0;
        int high = s.length() - 1;
        boolean flag = true;
        while(low < high){
            while(low < high && !Character.isLetterOrDigit(s.charAt(low))){//寻找<span style="color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 30px;">字母和数字的</span>
                low++;
            }
            while(low < high && !Character.isLetterOrDigit(s.charAt(high))){<span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">//寻找</span><span style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; line-height: 30px;">字母和数字的</span>
                high --;
            }
            if(low < high){
                if(s.charAt(low) != s.charAt(high)){
                    flag = false;
                    break;
                }else{
                    low ++;
                    high --;
                }
            }
        }
        return flag;
    }
}

Runtime: 270 ms

你可能感兴趣的:(LeetCode,valid,palindrome)