-
Notifications
You must be signed in to change notification settings - Fork 0
/
47.permutations-ii.cpp
33 lines (33 loc) · 1.01 KB
/
47.permutations-ii.cpp
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
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
if (nums.empty()) {
res.push_back(vector<int>());
}
sort(nums.begin(), nums.end());
vector<bool> visited(nums.size(), false);
vector<int> p;
helper(res, nums, p, visited);
return res;
}
void helper(vector<vector<int>> &res, vector<int> &nums, vector<int> &p, vector<bool> &visited) {
if (p.size() == nums.size()) {
res.push_back(p);
}
for (int i = 0; i < nums.size(); i++) {
if (visited[i]) {
continue;
}
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) {
continue;
}
visited[i] = true;
p.push_back(nums[i]);
helper(res, nums, p, visited);
p.pop_back();
visited[i] = false;
// while (i + 1 < nums.size() && nums[i] == nums[i + 1]) ++i;
}
}
};