Valid Palindrome

题目
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.

Example 1:
Input: "aba"
Output: True
Example 2:
Input: "abca"
Output: True
Explanation: You could delete the character 'c'.

Note: The string will only contain lowercase characters a-z. The maximum length of the string is 50000.

答案

class Solution {
    public boolean isPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while(left <= right) {
            char cleft = Character.toLowerCase(s.charAt(left));
            char cright = Character.toLowerCase(s.charAt(right));
            if(!Character.isDigit(cleft) && !Character.isLetter(cleft)) {
                left++;
                continue;
            }
            if(!Character.isDigit(cright) && !Character.isLetter(cright)) {
                right--;
                continue;
            }
            if(cleft != cright) return false;
            left++;
            right--;
        }
        return true;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容