privatevoiddfs(List<List<Integer>> resultList, Deque<Integer> path, int[] nums, int startIndex) { if (path.size() >= 2) { resultList.add(newArrayList<>(path)); }
Set<Integer> set = newHashSet<>(); for (inti= startIndex; i < nums.length; i++) { if (i > startIndex && set.contains(nums[i])) { continue; } set.add(nums[i]);
if (path.isEmpty() || nums[i] >= path.getLast()) { path.add(nums[i]); dfs(resultList, path, nums, i + 1); path.removeLast(); } } } }
注意此题不能排序,导致同层去重时不能使用 nums[i] == nums[i - 1] 判断,所以引入了 Set 来判断重复。