-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
34 lines (30 loc) · 796 Bytes
/
Permutations.cpp
File metadata and controls
34 lines (30 loc) · 796 Bytes
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
#include <bits/stdc++.h>
using namespace std;
void solve(vector<vector<int>> &pmt, vector<int> nums, int i) {
if (i == nums.size()) {
pmt.push_back(nums);
return;
}
for (int j = i; j < nums.size(); j++) {
if (i != j && nums[i] == nums[j]) continue;
swap(nums[i], nums[j]);
solve(pmt, nums, i + 1);
}
}
vector<vector<int>> permuteUnique(vector<int> &nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> pmt;
solve(pmt, nums, 0);
return pmt;
}
int main() {
int n;
cin >> n;
vector<int> nums(n);
for (int i = 0; i < n; i++) cin >> nums[i];
auto out = permute(nums);
for (auto x : out) {
for (auto y : x) cout << y << " ";
cout << endl;
}
}