-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47. Permutations II.cpp
More file actions
43 lines (35 loc) · 1.04 KB
/
47. Permutations II.cpp
File metadata and controls
43 lines (35 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution {
private:
void helper(vector<int>& nums, unordered_set<int> &visited, vector<int> &ans, vector<vector<int>>& res)
{
if (ans.size() == nums.size())
{
res.push_back(ans);
}
for (auto i = 0; i < nums.size(); ++i)
{
if (visited.count(i) != 0)
{
continue;
}
if (i > 0 && nums[i] == nums[i - 1] && visited.count(i - 1) == 0)
{
continue;
}
visited.insert(i);
ans.push_back(nums[i]);
helper(nums, visited, ans, res);
ans.pop_back();
visited.erase(i);
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
unordered_set<int> visited;
vector<vector<int>> res;
vector<int> ans;
helper(nums, visited, ans, res);
return res;
}
};