题目
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;
}
}