-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations.cpp
More file actions
51 lines (47 loc) · 1.09 KB
/
Permutations.cpp
File metadata and controls
51 lines (47 loc) · 1.09 KB
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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void dps(vector<int> &nums, int l, int r, vector<vector<int>> &result)
{
r = nums.size() - 1;
if (l == r)
{
result.push_back(nums);
return;
}
for (int i = l; i <= r; ++i)
{
int tmp = nums[l];
nums[l] = nums[i];
nums[i] = tmp;
dps(nums, l + 1, r, result);
int tmp2 = nums[l];
nums[l] = nums[i];
nums[i] = tmp2;
}
}
vector<vector<int>> permute(vector<int> &nums)
{
vector<vector<int>> result;
int firstIndex = 0;
int lastIndex = nums.size() - 1;
dps(nums, firstIndex, lastIndex, result);
return result;
}
};
class Solution
{
public:
vector<vector<int>> permute(vector<int> &nums)
{
vector<vector<int>> result;
sort(nums.begin(), nums.end());
do
{
result.push_back(nums);
} while (next_permutation(nums.begin(), nums.end()));
return result;
}
};