public boolean validPalindrome(String s) {
StringBuffer stringBuffer=new StringBuffer(s);
if(s.equals(stringBuffer.reverse().toString()))return true;
int low=0,high=s.length()-1;
while (low<high){
char c1=s.charAt(low),c2=s.charAt(high);
if(c1==c2){
low++;
high--;
}else{
return isValidate(s,low+1,high)||isValidate(s,low,high-1);
}
}
return true;
}
boolean isValidate(String s,int a,int b){
for (int i = a,j=b; i <j ; i++,j--) {
char c1=s.charAt(i),c2=s.charAt(j);
if(c1!=c2){
return false;
}
}
return true;
}
LeetCode简单题680.验证回文串Ⅱ
于 2022-09-13 17:13:49 首次发布