0
点赞
收藏
分享

微信扫一扫

90. 子集 II

倪雅各 2022-07-28 阅读 72


​​官方详细题解​​

90. 子集 II_leetcode


回溯法

class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
Arrays.sort(nums); //排序
getAns(nums, 0, new ArrayList<>(), ans);
return ans;
}

private void getAns(int[] nums, int start, ArrayList<Integer> temp, List<List<Integer>> ans) {
ans.add(new ArrayList<>(temp));
for (int i = start; i < nums.length; i++) {
//和上个数字相等就跳过
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
temp.add(nums[i]);
getAns(nums, i + 1, temp, ans);
temp.remove(temp.size() - 1);
}
}
}


举报

相关推荐

0 条评论