LeetCode 90. 子集 II
题目描述
思路分析
思路描述
参考代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
func subsetsWithDup(nums []int) [][]int {
if len(nums) == 0 {
return [][]int{}
}
sort.Ints(nums)
res := make([][]int, 0)
backtrack(nums, []int{}, &res, 0)
return res
}
func backtrack(nums []int, path []int, res *[][]int, start int) {
temp := make([]int, len(path))
copy(temp, path)
*res = append(*res, temp)
for i := start; i < len(nums); i++ {
if i > start && nums[i] == nums[i-1] {
continue
}
path = append(path, nums[i])
backtrack(nums, path, res, i+1)
path = path[:len(path)-1]
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
backtrack(nums, new ArrayList<>(), res, 0);
return res;
}
public void backtrack(int[] nums, List<Integer> path, List<List<Integer>> res, int start) {
res.add(new ArrayList<>(path));
for (int i = start; i < nums.length; i++) {
if (i > start && nums[i] == nums[i - 1]) {
continue;
}
path.add(nums[i]);
backtrack(nums, path, res, i + 1);
path.remove(path.size() - 1);
}
}
}
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用