题解 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
学习到的东西: Collections.sort()对一个list排序
比较两个stringBuilder要 strb.toString().equals();
输出list 要list.get
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String str = scan.nextLine();
getLength(str);
}
scan.close();
}
public static void getLength(String str){
List<Integer> list = new ArrayList<>();
for(int i = 0; i<str.length(); i++){
for(int j = i+1; j<=str.length(); j++){
StringBuilder strb = new StringBuilder();
strb.append(str.substring(i,j));
if(strb.toString().equals(strb.reverse().toString())){
list.add(j-i);
}
}
}
// for(int i = 0; i<list.size(); i++){
// System.out.print(" " + list.get(i));
// }
Collections.sort(list);
System.out.println(list.get(list.size()-1));
}
}