Problem
Given an integer array nums, return all possible subsets (the power set).
Part A (no duplicates)
Assume all elements in nums are distinct. Return all subsets in any order.
Part B (follow-up: may contain duplicates)
Now assume nums may contain duplicate values. Return all unique subsets (no duplicate subsets). Subsets can be returned in any order.
Input
-
nums
: an array of integers, length
n
Output
-
A list of lists, where each inner list is a subset of
nums
Constraints
-
0 <= n <= 20
-
-10 <= nums[i] <= 10
Examples
Example 1 (Part A)
Input: nums = [1,2,3]
Output (one valid ordering): [[],[1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]]
Example 2 (Part B)
Input: nums = [1,2,2]
Output (one valid ordering): [[],[1],[2],[1,2],[2,2],[1,2,2]]
Clarifications to ask
-
Are elements guaranteed distinct, or can there be duplicates?
-
If duplicates exist, should the output contain only unique subsets?