题解HJ85 | #最长回文子串#
最长回文子串
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 s = in.nextLine();
int maxLength = 0;
for (int i = 0; i < s.length(); i++) {
for (int j = i + 2; j <= s.length(); j++) {
if (check(s.substring(i, j))) {
if (j - i - 1 > maxLength) {
maxLength = j - i;
}
}
}
}
System.out.println(maxLength);
}
public static boolean check(String s) {
boolean flag = false;
if (s.length() == 1) {
flag = true;
return flag;
} else if (s.length() == 2 && s.charAt(0) == s.charAt(1)) {
flag = true;
return flag;
} else {
if (s.charAt(0) == s.charAt(s.length() - 1)) {
return check(s.substring(1, s.length() - 1));
}
return flag;
}
}
}
很简单 做的很顺

