-
Notifications
You must be signed in to change notification settings - Fork 0
/
PermutationsII2.cpp
86 lines (61 loc) · 1.59 KB
/
PermutationsII2.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
vector<vector<int> > permuteUnique(vector<int>& nums) {
vector<vector<int> > result;
vector<int> one;
result.push_back(one);
for(int i=0;i<nums.size();i++){
vector<vector<int> > temp;
for(int j=0;j<result.size();j++){
for(int k=0;k<=i;k++){
vector<int> one=result[j];
one.insert(one.begin()+k,nums[i]);
temp.push_back(one);
if(k<result[j].size()&&result[j][k]==nums[i]){
break;
}
}
}
//for(int j=0;j<=i;j++){
// for(int k=0;k<result.size();k++){
// vector<int> one=result[k];
// if((!(j<one.size()&&one.at(j)==nums[i]))){
// one.insert(one.begin()+j,nums[i]);
// temp.push_back(one);
// }
// }
//}
result=temp;
cout<<"----------"<<endl;
for(int i=0;i<result.size();i++){
for(int j=0;j<result[i].size();j++){
cout<<result[i][j]<<", ";
}
cout<<endl;
}system("pause");
}
return result;
}
};
int main(){
int n;
cin>>n;
vector<int> nums;
for(int i=0;i<n;i++){
int num;
cin>>num;
nums.push_back(num);
}
Solution *solution=new Solution();
vector<vector<int> > result=solution->permuteUnique(nums);
for(int i=0;i<result.size();i++){
for(int j=0;j<result[i].size();j++){
cout<<result[i][j]<<", ";
}
cout<<endl;
}
return 0;
}