Given a string s, return true if the s can be palindrome after deleting at most one character from it.
思路
用头尾指针遍历原字符串,但碰到所指不相同时,需要退出循环记录此书指针的位置。分别去除两个指针上的内容,查看删除一个字符后的字符串是否为回文串。
代码
class Solution {
public:bool validPalindrome(string s) {int len = s.size();int head = 0, tail = len-1;while(head < tail){if(s[head] != s[tail])break;head++;tail--;}if(ifpalindrome(s, head))return true;else if(ifpalindrome(s, tail))return true;return false;}bool ifpalindrome(string s, int index){s.erase(index, 1);int head = 0, tail = s.size()-1;while(head < tail){if(s[head] != s[tail])return false;head++;tail--;}return true;}
};