参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { String s = scanner.next(); int n = s.length(); if(n < 2) { System.out.println(n); continue; } int start = 0, end = 0; int res = 1; for(int i = 0; i < n; i++) { int len1 = find(s, i, i); int len2 = find(s, i, i + 1); res = Math.max(res, Math.max(len1, len2)); } System.out.println(res); } } public static int find(String s, int i, int j) { while(i >= 0 && j < s.length() && s.charAt(i) == s.charAt(j)) { i--; j++; } return j - i - 1; } }
0.0分
4 人评分