Top 150 Interview簡単

Subsets II

Detailed guide and Python implementation for the 'Subsets II' problem.

問題提起

簡単

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.

Implement a function subsetsWithDup(nums: list) -> list.

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

Example 1
Input
[1,2,2]
Output
[[],[1],[1,2],[1,2,2],[2],[2,2]]
Explanation

Unlike the basic subsets problem, [1,2,2] has duplicate 2's. We skip duplicate subsets like having two copies of [2].

Example 2
Input
[0]
Output
[[],[0]]
Explanation

Same as basic subsets for a single element.

Need a Hint?
Consider using Backtracking-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

解決する準備はできましたか?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

エディタで開く
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。