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'.
思路:
一旦不相等,就扩展一位来看结果。这个思路挺独特的。
l本来就+1,所以这里应该是-1。char不相等,表示让1位判断整个是不是Palindromic。不行就整个退了
++l < --r:这一题要先加再赋值
class Solution { public boolean validPalindrome(String s) { //cc if (s == null || s == "") return true; int l = -1, r = s.length(); while (++l < --r) { if (s.charAt(l) != s.charAt(r)) return isPalindrome(s, l - 1, r) || isPalindrome(s, l, r + 1); } return true; } public boolean isPalindrome(String s, int l, int r) { while (++l < --r) { if (s.charAt(l) != s.charAt(r)) return false; } return true; } }