题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
非栈,复杂
import java.util.*;
public class Main{
public static void main(String[]args){
Scanner input = new Scanner(System.in);
while(input.hasNextLine()){
String a = input.nextLine();
int sum = 0;
int alre=0;
if(isHuiWen(a)!=0){
sum = isHuiWen(a);
System.out.println(sum);
alre = 1;
}
for(int j = 0; j<a.length();j++){
for(int i = 0; i<a.length()-j;i++){
if(isHuiWen(a.substring(j,a.length()-1-i)) > sum){
sum = isHuiWen(a.substring(j,a.length()-1-i));
}
}
}
if(sum <= 1){
sum = 1;
}
if(alre == 0){
System.out.println(sum);
}
}
}
public static int isHuiWen(String a){
int flag = a.length();
if(flag % 2 == 0){ //双数长度
for(int i = 0; i < a.length()/2; i++){
if(a.charAt(i)!=a.charAt(a.length()-i-1)){
flag = 0;
return flag;
}
}
}
else{ //单数长度
for(int i = 0; i < a.length()/2; i++){ //这个长度可能不是int,出错了直接floor
if(a.charAt(i)!=a.charAt(a.length()-i-1)){
flag = 0;
return flag;
}
}
}
return flag;
}
}