题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case String s = in.next(); int length=s.length(); int count=0; for(int i=0;i<length;i++){ for(int j=length;j>i;j--){ String temp=s.substring(i,j); if(check(temp)){ if(count<(j-i)){ count=j-i; } //如果截取的字符串是回文串,不再对j以前的进行判断 break; } } } System.out.println(count); } } //利用java自带的api进行是否为回文(对称)的判断 public static boolean check(String s){ StringBuilder sb=new StringBuilder(s); String mirror=sb.reverse().toString(); return s.equals(mirror); } }