-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLecture39.cpp
More file actions
41 lines (30 loc) · 818 Bytes
/
Copy pathLecture39.cpp
File metadata and controls
41 lines (30 loc) · 818 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
37
38
39
40
41
#include<iostream>
#include<vector>
using namespace std;
class Solution {
private:
void solve(vector<int> nums, int index, vector<vector<int> >& ans) {
//base case
if(index >= nums.size()) {
ans.push_back(nums);
return ;
}
for(int i=0; i<nums.size(); i++) {
swap(nums[index],nums[i]);
// Recursion
solve(nums, index+1, ans);
// back tracking
swap(nums[index],nums[i]);
}
}
public:
vector<vector<int>> permute(vector<int>& nums) {
vector<vector<int> > ans;
int index = 0;
solve(nums, index, ans);
return ans;
}
};
int main(){
return 0;
}