题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
1.for循环嵌套截取子串;
2.判断子串是否是回文字符串;
3.计算个子串中长度的最大值;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
String str = sc.nextLine();
//StringBuilder sb = new StringBuilder();
//sb.append(str);
//sb.reverse();
int max = 0;
for(int i = 0 ;i < str.length(); i++){
for(int j = i + 1; j <= str.length(); j++){
StringBuilder sb = new StringBuilder();
sb.append(str.substring(i,j));
sb.reverse();
if(str.substring(i,j).contentEquals(sb)){
if(j - i > max){
max = j - i;
}
}
}
}
System.out.println(max);
}
}
} 