forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.cpp
More file actions
31 lines (26 loc) · 887 Bytes
/
46.cpp
File metadata and controls
31 lines (26 loc) · 887 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
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int>> permutations;
permutate(nums, nums.size(), permutations);
return permutations;
}
void permutate(vector<int>& values, int size, vector<vector<int>>& permutations) {
// Check if is a valid value
if (size <= 0 && size > values.size())
return;
// Get permutation
if (size == 1) {
permutations.push_back(values);
} else {
// Permutate
for (int i = 0; i < size; i++) {
permutate(values, size - 1, permutations);
if (size % 2 == 1)
swap(values[0], values[size - 1]);
else
swap(values[i], values[size - 1]);
}
}
}
};