1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| class Solution { public List<List<Integer>> findMatrix(int[] nums) { Map<Integer, Integer> numToCountMap = new HashMap<>(); int maxCount = 0; for (int num : nums) { int count = numToCountMap.getOrDefault(num, 0) + 1; numToCountMap.put(num, count); maxCount = Math.max(maxCount, count); }
List<List<Integer>> resultList = new ArrayList<>(maxCount); for (int i = 0; i < maxCount; i++) { resultList.add(new ArrayList<>()); } for (Map.Entry<Integer, Integer> entry : numToCountMap.entrySet()) { int num = entry.getKey(), count = entry.getValue(); for (int i = 0; i < count; i++) { resultList.get(i).add(num); } } return resultList; } }
|
References
2610. Convert an Array Into a 2D Array With Conditions