手写几个测试用例就知道了规律
0123456789 ==11
01123456789==22
0112233445566789=77
00112233445566778899=100
00112233445566778899==111
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); int[] ints = new int[10]; for(int i = 0; i < s.length(); i++){ ints[Integer.parseInt(String.valueOf(s.charAt(i)))] += 1; } //输出结果 ints[0]++;//0加一位 int min = Integer.MAX_VALUE, index = -1; for(int i = ints.length - 1; i >= 0; i--){ if(ints[i] < min){ min = ints[i]; index = min; } } String res = ""; for(int i = 0; i < min; i++){ res += index; } if(index == 0){ res = 1 + res; }else{ res = index + res; } System.out.println(res); } }