1.双指针
代码如下:
class Solution {
public:
bool validPalindrome(string s) {
//首先确定双指针,i从前往后,j从后往前
//当s[i]!=s[j],判断(i+1,j) (i,j-1)是否有一个回文串
int i=0,j=s.size()-1;
while(i<j)
{
if(s[i]!=s[j])
return huiwen(s,i+1,j)||huiwen(s,i,j-1);
i++;
j--;
}
return true;
}
bool huiwen(string s,int i,int j)
{
while(i<j)
{
if(s[i]!=s[j])
return false;
i++;
j--;
}
return true;
}
};