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.
题目链接:https://leetcode-cn.com/problems/valid-palindrome-ii/
思路
简单的判断,逻辑直接翻译代码。
class Solution {
public:
bool validPalindrome(string s) {
int l = 0, r = s.size()-1;
while(l<r){
if(s[l]!=s[r]){
return valid(s, l, r-1) || valid(s, l+1, r);
}
++l;
--r;
}
return true;
}
bool valid(string s, int l, int r){
while(l<r){
if(s[l]!=s[r]){
return false;
}
++l;
--r;
}
return true;
}
};