第一题
统计加排序
第二题
暴力dfs+去重
第三题
没做出来
第一题
class Solution { public int[] frequencySort(int[] nums) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { map.put(nums[i], map.getOrDefault(nums[i], 0) + 1); } int[][] count = new int[map.size()][2]; int[] res = new int[nums.length]; int idx = 0; for (int key : map.keySet()) { count[idx][0] = key; count[idx][1] = map.get(key); idx++; } Arrays.sort(count, (a, b) -> { if (a[1] != b[1]) { return a[1] - b[1]; } else { return b[0] - a[0]; } }); idx = 0; for (int i = 0; i < count.length; i++) { int temp = 0; while (temp++ < count[i][1]) { res[idx++] = count[i][0]; } } return res; } }
第二题
class Solution { List<List<Integer>> res = new ArrayList<List<Integer>>(); Set<String> visited = new HashSet<String>(); Stack<Integer> stack = new Stack<Integer>(); public List<List<Integer>> findSubsequences(int[] nums) { dfs(0, nums); return res; } private void dfs(int idx, int[] nums) { if (idx == nums.length) { if (stack.size() >= 2) { List<Integer> temp = new ArrayList<Integer>(stack); String seq = temp.stream().map(String::valueOf).collect(Collectors.joining(",")); if (!visited.contains(seq)) { res.add(temp); visited.add(seq); } } return; } if (stack.isEmpty() || nums[idx] >= stack.peek()) { stack.push(nums[idx]); dfs(idx + 1, nums); stack.pop(); } dfs(idx + 1, nums); } }