-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutation.cpp
77 lines (76 loc) · 1.34 KB
/
permutation.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
// Given an array nums of distinct integers,return all the possible permutations.You can return the answer in any order.
/*
Sample Input
3
1 2 3
Sample output
123
132
213
231
321
312
*/
/*
**sodo-code**
if(idx == nums.size()){
ans.push_back(nums);
return;
}
for(int i=idx;i<nums.size();i++){
swap(nums[i],nums[idx]);
solve(nums,ans,idx+1);
swap(nums[i],nums[idx]);
}
*/
#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> ans;
void permute(vector<int> &a, int idx)
{
if (idx == a.size()) // base case
{
ans.push_back(a);
return;
}
for (int i = idx; i < a.size(); i++)
{
swap(a[i], a[idx]);
permute(a, idx + 1);
swap(a[i], a[idx]);
}
return;
}
int32_t main()
{
int n;
cin >> n;
vector<int> a(n);
// input n integer
for (int i = 0; i < a.size(); i++)
{
cin >> a[i];
}
/*
for (auto &i : a)
cin >> i;
*/
sort(a.begin(), a.end());
do
{
ans.push_back(a);
} while (next_permutation(a.begin(), a.end()));
// syntex
// for ( range_declaration : range_expression )
// loop_statement
int x=0;
for (auto v : ans)
{
for (auto i : v)
cout << i << "";
cout << "\n";
x++;
}
cout << "Permutation Number: " << x << endl;
}
//Complexity: 0(N)