-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutations
More file actions
36 lines (31 loc) · 806 Bytes
/
Copy pathPermutations
File metadata and controls
36 lines (31 loc) · 806 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
35
36
#include <iostream>
#include "vector"
using namespace std;
/*
* Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.
*/
class Solution {
vector<vector<int>> v;
public:
vector<vector<int>> permute(vector<int>& nums) {
this->recursive(nums,0);
return this->v;
}
void recursive(vector<int>& nums, int index){
if (nums.size() == index){
this->v.push_back(nums);
return;
}
for (int i = index; i < nums.size(); ++i) {
swap(nums[i],nums[index]);
recursive(nums,index+1);
swap(nums[i],nums[index]);
}
}
};
int main() {
vector<int> a = {1,2,3};
Solution b = Solution();
b.permute(a);
return 0;
}