-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permuteUnique.cpp
54 lines (50 loc) · 1.5 KB
/
permuteUnique.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Approach 1 - Set
class Solution {
private:
void permute(set<vector<int>>& s, vector<int>& nums, int start) {
if(start == nums.size()) {
s.insert(nums);
} else {
for(int i = start; i < nums.size(); ++i) {
swap(nums[i], nums[start]);
permute(s, nums, start + 1);
swap(nums[i], nums[start]);
}
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> s;
permute(s, nums, 0);
vector<vector<int>> result(s.begin(), s.end());
return result;
}
};
// Approach Two - Map to avoid unnecessary calls
class Solution {
private:
void permute(map<int, int>& m, vector<vector<int>>& result, vector<int>& temp, int start) {
if(start == temp.size()) {
result.push_back(vector<int>(temp.begin(), temp.end()));
} else {
for(auto &x: m) {
if(x.second > 0) {
m[x.first] = x.second - 1;
temp[start] = x.first;
permute(m, result, temp, start + 1);
m[x.first] = x.second + 1;
}
}
}
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
map<int, int> m;
vector<int> temp(nums.size());
for(int num: nums)
m[num]++;
permute(m, result, temp, 0);
return result;
}
};