判断一个数中最大回文数的长度 :例如12332112345654321中最大的回文数是12345654321,长度为11
public static void palindrome(String str) { int len = str.length(); int max = 1; for(int i = 1; i < len; i++){ int low = i-1; //偶数情况 int high = i; while(low >= 0 && high < len && str.charAt(low) == str.charAt(high)){ low--; high++; } if(high-low-1 > max){ max = high-low-1; } low = i-1; //奇数情况 high = i+1; while(low >= 0 && high < len && str.charAt(low) == str.charAt(high)){ low--; high++; } if(high-low-1 > max){ max = high-low-1; } } System.out.println(max); } public static void main(String[] args) { String s = "1234321123565321"; palindrome(s); }
时间: 2024-10-12 05:10:30