-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47.permutations-ii.cpp
More file actions
37 lines (32 loc) · 869 Bytes
/
47.permutations-ii.cpp
File metadata and controls
37 lines (32 loc) · 869 Bytes
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
class Solution {
private:
vector<vector<int>> result;
vector<int> current;
int n;
void permute(int length, vector<int>& nums) {
if(length == n) {
result.push_back(current);
return;
}
for(int i = 0; i < n; i++) {
int val = nums[i];
if(val != INT_MIN) {
current.push_back(val);
nums[i] = INT_MIN;
permute(length + 1, nums);
nums[i] = val;
current.pop_back();
while(i < n - 1 && nums[i] == nums[i + 1]) {
i++;
}
}
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
n = nums.size();
permute(0, nums);
return result;
}
};