[leetcode]680. Valid Palindrome II
Analysis
嘻嘻~—— [嘻嘻~]
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
判断删了一个字符之后是不是回文串,当然如果本来就是回文串更好~
Implement
class Solution {
public:
bool validPalindrome(string s) {
int len = s.length();
int left = 0;
int right = len-1;
while(left < right){
if(s[left] != s[right])
return isPalindrome(s, left+1, right) || isPalindrome(s, left, right-1);
left++;
right--;
}
return true;
}
bool isPalindrome(string s, int left, int right){
while(left < right){
if(s[left] != s[right])
return false;
left++;
right--;
}
return true;
}
};