Valid Palindrome

2019-02-05  本文已影响0人  乔田伟
public class Solution {
    /**
     * @param s: A string
     * @return: Whether the string is a valid palindrome
     */
    public boolean isPalindrome(String s) {
        // write your code here
        s = s.toLowerCase();
        int i = 0, j = s.length() - 1;
        while(i < j) {
            if(!Character.isLetterOrDigit(s.charAt(i))) {
                i++;
                continue;
            }
            if(!Character.isLetterOrDigit(s.charAt(j))) {
                j--;
                continue;
            }
            char leftChar = s.charAt(i++);
            char rightChar = s.charAt(j--);
            if(leftChar != rightChar) {
                return false;
            } 
        }
        return true;
        
    }
}
上一篇 下一篇

猜你喜欢

热点阅读