90. Subsets II
Problem description:
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.1
2
3
4
5
6
7
8
9
10
11
12Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
Solution:
This question is very similar to 39. Combination Sum and 78. Subsets.
- Sort the array
- When recursion, we need to check for the duplicate elements, if
i
is equal to previous item then bypass it - A major difference is that we need to use
i
to do the backtrack, instead ofpos+1
in combination.
1 | class Solution { |