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.

Example:

Input: [1,2,2]
Output:
[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

algorithm thought

得到子集问题,和之前的第一题相比,多了重复数字,在计算子集的时候,需要处理重复情况。这里可以用和第一题类似的回溯法。也可以一次次将新数字push_back到之前得到的结果中,得到新的子集。

code

algorithm analysis

得到所有的子集问题,回溯法时间复杂度O(2^n)。每个数字可以出现一个也可以不出现,每个数字都会进行两次计算,两个分支。最后得到2^n

Last updated