Problem
Problem page: https://leetcode.com/problems/subsets-ii/
Topics: Array, Backtracking
Difficulty: Medium
Given a collection of integers that might contain duplicates,nums, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If nums =[1,2,2], a solution is:[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
Analysis
This is pretty similar to Subsets. I sort the input list S first. Keeping track of the previous character in each recursive call is the only change I need. If two characters are equal and I skipped the earlier one, I can't take the later one either, which keeps me from generating duplicate subsequences.
Code
1 | class Solution: |
Article link: https://xnerv.wang/en/leetcode-subsets-ii/
