LeetCode 0090. Subsets II Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0090. Subsets II

Description

Given an integer array nums that may contain duplicates, return all possible subsets (the power set).

The solution set must not contain duplicate subsets. Return the solution in any order.

 

Example 1:

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

Example 2:

Input: nums = [0]
Output: [[],[0]]

 

Constraints:

  • 1 <= nums.length <= 10
  • -10 <= nums[i] <= 10

Solutions

Solution 1: Sorting + DFS

We can first sort the array nums to facilitate deduplication.

Then, we design a function dfs(i), which represents the current search for subsets starting from the i-th element. The execution logic of the function dfs(i) is as follows:

If i ≥ n, it means all elements have been searched, add the current subset to the answer array, and end the recursion.

If i < n, add the i-th element to the subset, execute dfs(i + 1), then remove the i-th element from the subset. Next, we check if the i-th element is the same as the next element. If they are the same, skip the element in a loop until we find the first element different from the i-th element, then execute dfs(i + 1).

Finally, we only need to call dfs(0) and return the answer array.

The time complexity is O(n × 2^n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptRustJavaScriptC#
class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: def dfs(i: int): if i == len(nums): ans.append(t[:]) return t.append(nums[i]) dfs(i + 1) x = t.pop() while i + 1 < len(nums) and nums[i + 1] == x: i += 1 dfs(i + 1) nums.sort() ans = [] t = [] dfs(0) return ans(code-box)

Solution 2: Sorting + Binary Enumeration

Similar to Solution 1, we first sort the array nums to facilitate deduplication.

Next, we enumerate a binary number mask in the range [0, 2^n), where the binary representation of mask is an n-bit bit string. If the i-th bit of mask is 1, it means selecting nums[i], and 0 means not selecting nums[i]. Note that if the (i - 1)-th bit of mask is 0 and nums[i] = nums[i - 1], it means that the i-th element is the same as the (i - 1)-th element in the current enumeration scheme. To avoid duplication, we skip this case. Otherwise, we add the subset corresponding to mask to the answer array.

After the enumeration, we return the answer array.

The time complexity is O(n × 2^n), and the space complexity is O(n). Here, n is the length of the array nums.

PythonJavaC++GoTypeScriptRustJavaScriptC#
class Solution: def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: nums.sort() n = len(nums) ans = [] for mask in range(1 << n): ok = True t = [] for i in range(n): if mask >> i & 1: if i and (mask >> (i - 1) & 1) == 0 and nums[i] == nums[i - 1]: ok = False break t.append(nums[i]) if ok: ans.append(t) return ans(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !