题解 | #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); int length = 0; for (int i = 0; i < str.length(); i++) { for (int j = i; j <= str.length(); j++) { if (istrue(str.substring(i, j))) { if (length < str.substring(i, j).length()) { length = str.substring(i, j).length(); } } } } System.out.println(length); } public static boolean istrue(String str) { if (str.length() == 0) { return false; } int start = 0; int end = str.length() - 1; while ( start < end ) { char char_start = str.charAt(start); char char_end = str.charAt(end); if (char_start != char_end) { return false; } start++; end--; } return true; } }