# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
solve.cpp

## Subsets II

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],
[]
]
``````

## Solution

`[1,2,2,2]`为例，前面的1,2均无重复，因此按照正常过程走，得到result为

``````[]
[1]
[2]
[1,2]
``````

``````[]
[1]
**********以下为不重复部分*******
[2]
[1,2]
``````

``````[]
[1]
[2]
[1,2]
[2,2]
[1,2,2]
``````

``````[]
[1]
[2]
[1,2]
**********以下为不重复部分*******
[2,2]
[1,2,2]
``````

Subsets中，算法为:

```for (int i = 0; i < n; ++i) {
int m = result.size();
for (int j = 0; j < m; ++j) {
result.push_back(result[j]);
result.back().push_back(nums[i]);
}
}
```

`j = i > 0 && nums[i] == nums[i - 1] ? lastSize : 0;`

```vector<vector<int>> subsets_iterative(vector<int> &nums) {
vector<vector<int>> result(1, vector<int>());
sort(begin(nums), end(nums));
int n = nums.size();
int m = 0;
for (int i = 0; i < n; ++i) {
int j = i > 0 && nums[i] == nums[i - 1] ? m : 0;
m = result.size();
for (; j < m; ++j) {
result.push_back(result[j]);
result.back().push_back(nums[i]);
}
}
return result;
}```
You can’t perform that action at this time.