Valid Palindrome(有效回文串)

问题

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Notice

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.
Example
"A man, a plan, a canal: Panama" is a palindrome.

"race a car" is not a palindrome.

分析

两边同时开始,不是数字或字母的就跳过,是的就比较。

代码

public class Solution {
    /**
     * @param s A string
     * @return Whether the string is a valid palindrome
     */
    public boolean isPalindrome(String s) {
        // Write your code here
        int l=0;
        int r=s.length()-1;
        while(l=r){
                break;
            }
            char a=s.charAt(l);
            char b=s.charAt(r);
            if(Character.toLowerCase(a)!=Character.toLowerCase(b)){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
}

你可能感兴趣的:(Valid Palindrome(有效回文串))