-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path046_Permutations.cpp
113 lines (108 loc) · 2.76 KB
/
046_Permutations.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include<vector>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
class Solution
{
public:
vector<vector<int>> permute2(vector<int>& nums)
{
size_t n = nums.size();
size_t res = pow(n,n);
size_t target_sum=0;
vector<vector<int>> resv;
for(int i = 0; i < n;i++)
target_sum+= i;
vector<int> permute_bit(n,0);
int temp,temp2,j,remainder;
for(int i =0; i < res; i++)
{
temp=i;
temp2=j=0;
while(temp != 0)
{
remainder = temp % n;
temp = temp / n;
permute_bit[j++] = remainder;
temp2 += remainder;
}
if(temp2 == target_sum)
{
bool add = true;
for(size_t k = 0; k < n; k++)
{
if(find(permute_bit.begin()+k+1,permute_bit.end(), permute_bit[k]) != permute_bit.end())
add=false;
}
if(add)resv.push_back(permute_bit);
}
}
cout<<resv.size()<<endl;
vector<int> tempv(n,0);
for(size_t l = 0; l < resv.size();l++)
{
for(int x = 0; x < n;x++)
tempv[x] = nums[resv[l][x]];
resv[l]=tempv;
}
for(auto e:resv)
{
for(auto ee: e)
cout<<ee<<"\t";
cout<<endl;
}
return resv;
}
vector<vector<int>> permute(vector<int>& nums)
{
sort(nums.begin(),nums.end());
this->nums = nums;
this->L = this->nums.size();
vector<int> t(this->L);
helper(t,0);
return this->res;
}
void helper(vector<int>& tmp,int cur)
{
if( cur == L)
{
this->res.push_back(tmp);
return;
}
auto tt = this->nums;
for(int i = 0; i < cur; i++)
{
auto f = find(tt.begin(),tt.end(), tmp[i]);
if( f != tt.end())
{
tt.erase(f);
}
}
for(int i = 0; i < tt.size();i++)
{
if(i>=1 && tt[i] == tt[i-1])continue;
auto e = tt[i];
tmp[cur]=e;
helper(tmp,cur+1);
}
}
private:
int L;
vector<int> nums;
vector<vector<int>> res;
};
int main(int argc, char const *argv[])
{
vector<int> nums = {1,1,2};
//{1,2,2,2,3,4,4,5,7};
Solution s;
// s.permute(nums);
for(auto e:s.permute(nums))
{
for(auto ee: e)
cout<<ee<<"\t";
cout<<endl;
}
return 0;
}