美文网首页
Valid Palindrome

Valid Palindrome

作者: 乔田伟 | 来源:发表于2019-02-05 10:59 被阅读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;
        
    }
}

相关文章

网友评论

      本文标题:Valid Palindrome

      本文链接:https://www.haomeiwen.com/subject/gsxvsqtx.html