-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_47_Permutations2.cpp
61 lines (48 loc) · 1.37 KB
/
LC_47_Permutations2.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
55
56
57
58
59
60
61
/*
https://leetcode.com/problems/permutations-ii/
47. Permutations II
*/
class Solution {
public:
vector<vector<int>> ans;
int high_ind;
set<vector<int>> s;
vector<vector<int>> permuteUnique(vector<int>& nums) {
high_ind = nums.size()-1;
findPermute(0, nums);
return ans;
}// end
void findPermute(int ind, vector<int>& nums)
{
if(ind==high_ind)
{
// if(find(ans.begin(),ans.end(),nums)==ans.end()){
// ans.push_back(nums);
// } // 250ms+
if(s.find(nums)==s.end())
{ s.insert(nums);
ans.push_back(nums);
}//19ms+
// ans.push_back(nums);
return;
}
bool flag=true;
for(int i=ind; i<=high_ind; i++)
{
if(ind !=i && nums[ind]==nums[i]) continue;
swap(nums[i],nums[ind]);
findPermute(ind+1, nums);
swap(nums[i],nums[ind]);
}
}
vector<vector<int>> permuteUnique1(vector<int>& nums)
{
sort(nums.begin(), nums.end());
vector<vector<int>>ans;
ans.push_back(nums);
while(next_permutation(nums.begin(), nums.end())){
ans.push_back(nums);
}
return ans;
}
};