680. Valid Palindrome IIeasy
Posted abc_begin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了680. Valid Palindrome IIeasy相关的知识,希望对你有一定的参考价值。
680. Valid Palindrome II【easy】
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.
错误解法:
1 class Solution { 2 public: 3 bool validPalindrome(string s) { 4 int start = 0; 5 int end = s.length() - 1; 6 int flag = false; 7 8 while (start <= end) { 9 if (s[start] == s[end]) { 10 ++start; 11 --end; 12 } 13 else { 14 if (!flag) { 15 flag = true; 16 17 if (s[start + 1] == s[end]) { 18 ++start; 19 } else if (s[start] == s[end - 1]) { 20 --end; 21 } else { 22 return false; 23 } 24 } 25 else { 26 return false; 27 } 28 } 29 } 30 31 return true; 32 33 } 34 };
没有通过以下测试用例:
"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga"
考察代码逻辑,发现一开始我们命中了
s[start + 1] == s[end]
这个逻辑,"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga",并且设置了标志位,这就坑了,下次发现不相等的情况,就直接返回false了。
其实我们本意是要命中下面的逻辑
s[start] == s[end - 1]
"aguokepatgbnvfqmgmlcupuufxoohdfpgjdmysgvhmvffcnqxjjxqncffvmhvgsymdjgpfdhooxfuupuculmgmqfvnbgtapekouga",并且设置了标志位,这样就可以满足测试用例。
就是说每次我们用
s[start + 1] == s[end]
和
s[start] == s[end - 1]
其实是一种“或”的关系,所以我们要一判断就判断到底,比如下面的解法一。
解法一:
1 class Solution { 2 public boolean validPalindrome(String s) 3 { 4 int left = 0, right = s.length() - 1; 5 6 while (left < right) 7 { 8 if (s.charAt(left) == s.charAt(right)) 9 { 10 left++; right--; 11 } 12 else 13 { 14 //remove right 15 int templeft = left, tempright = right - 1; 16 17 while (templeft < tempright) 18 { 19 if (s.charAt(templeft) != s.charAt(tempright)) break; 20 templeft++; tempright--; 21 22 if (templeft >= tempright) return true; 23 } 24 25 //remove left 26 left++; 27 28 while (left < right) 29 { 30 if (s.charAt(left) != s.charAt(right)) return false; 31 left++; right--; 32 } 33 } 34 } 35 return true; 36 } 37 }
参考@yashar 的代码。
解法二:
1 class Solution { 2 public boolean validPalindrome(String s) 3 { 4 return validPalindrome(s, 0, s.length() - 1, false); 5 } 6 7 private boolean validPalindrome(String s, int left, int right, Boolean mismatch) 8 { 9 if (right < left) return true; 10 11 if (s.charAt(left) != s.charAt(right)) 12 { 13 if (mismatch == true) 14 return false; 15 16 return validPalindrome(s, left, right - 1, true) || validPalindrome(s, left + 1, right, true); 17 } 18 else 19 { 20 return validPalindrome(s, left + 1, right - 1, mismatch); 21 } 22 } 23 }
递归,参考@yashar 的代码。
解法三:
1 class Solution { 2 public: 3 bool validPalindrome(string s) { 4 return valid(s, 0, s.length() - 1, 1); 5 } 6 7 private: 8 bool valid(string& s, int i, int j, int d) { // d: num of chars you can delete at most 9 if (i >= j) return true; 10 if (s[i] == s[j]) 11 return valid(s, i + 1, j - 1, d); 12 else 13 return d > 0 && (valid(s, i + 1, j, d - 1) || valid(s, i, j - 1, d - 1)); 14 } 15 };
递归,参考@alexander 的代码。
以上是关于680. Valid Palindrome IIeasy的主要内容,如果未能解决你的问题,请参考以下文章
Leetcode_easy680. Valid Palindrome II
算法:680. Valid Palindrome II验证回文||