题解 | #密码截取# 中心拓展法
密码截取
https://www.nowcoder.com/practice/3cd4621963e8454594f00199f4536bb1
import java.io.*; import java.util.*; public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); String str=sc.nextLine(); int maxLong=0; for(int i=0;i<str.length();i++){ maxLong = Math.max(calculate(str,i,i),maxLong); maxLong = Math.max(calculate(str,i,i+1),maxLong); } System.out.print(maxLong); } private static int calculate(String str,int l,int r){ int len=0; while(l>=0&&r<str.length()){ if(str.charAt(l)==str.charAt(r)){ len=Math.max(len,r-l+1); }else{ break; } l--; r++; } return len; } }