方法一:
直接使用 StringBuilder类提供的反转字符串的方法
//判断回文(stringBuffer版)
public static boolean isTenet(String str){
StringBuilder builder = new StringBuilder(str);
String str_rev = builder.reverse().toString();
if (str.equals(str_rev)){
return true;
}else {
return false;
}
}
方法二:
从两头往中间遍历
//判断回文(两头往中间)
public static boolean isTenet2(String str){
int top = 0;
int end = str.length() -1;
while (top < end){
if (str.charAt(top) != str.charAt(end)){
return false;
}
top++;
end--;
}
return true;
}
}