-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permutations.cpp
36 lines (36 loc) · 1022 Bytes
/
Permutations.cpp
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
class Solution {
public:
vector<vector<int>> ans;
vector<vector<int>> permuteUtil(vector<int> soFar, vector<int> remaining) {
if(remaining.size() == 0){
ans.push_back(soFar);
return ans;
}
for(int i = 0; i < remaining.size(); i++)
{
soFar.push_back(remaining[i]);
vector<int> rest;
for(int j = 0; j < remaining.size(); j++)
{
if(i == j)
continue;
rest.push_back(remaining[j]);
}
permuteUtil(soFar, rest);
soFar.erase(soFar.begin() + soFar.size() - 1);
}
return ans;
}
vector<vector<int>> permute(vector<int>& nums) {
int n = nums.size();
if(n == 0)
return ans;
vector<int> soFar;
vector<int> remaining;
for(int i = 0; i < n; i++)
{
remaining.push_back(nums[i]);
}
return permuteUtil(soFar, remaining);
}
};