Java 最优题解| #最长回文子串#
最长回文子串
https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner input = new Scanner(System.in); String s = input.nextLine(); int max = 0; for (int i = 0; i < s.length(); i++) for (int j = s.length() - 1; j - i > max; j--) if (isPalindromeString(s, i, j)) max = Math.max(max, j - i + 1); System.out.print(max); } static boolean isPalindromeString(String s, int head, int tail) { while (head < tail) if (s.charAt(head++) != s.charAt(tail--)) return false; return true; } }
双重for剪枝 + 无复制判断