方法:分组 + 排序 class Solution { public int largestInteger(int num) { List<Integer> ji = new ArrayList<>(); List<Integer> ou = new ArrayList<>(); String number = "" + num; for (int i = 0; i < number.length(); i++) { int n = number.charAt(i) - '0'; if (n % 2 == 1) { ji.add(n); } else { ou.add(n); } } Collections.sort(ji, (o1, o2) -> o2 - o1); Collections.sort(ou, (o1, o2) -> o2 - o1); StringBuilder sb = new StringBuilder(); for (int i = 0; i < number.length(); i++) { int n = number.charAt(i) - '0'; if (n % 2 == 1) { sb.append(ji.get(0)); ji.remove(0); } else { sb.append(ou.get(0)); ou.remove(0); } } return Integer.parseInt(sb.toString()); } }