forked from PawanJaiswal08/leetcode-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path46.Permutations.cpp
More file actions
32 lines (32 loc) · 792 Bytes
/
46.Permutations.cpp
File metadata and controls
32 lines (32 loc) · 792 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
class Solution
{
public:
void generate(vector<int> nums, vector<int> map, vector<vector<int>> &ans, vector<int> temp)
{
if (temp.size() == nums.size())
{
ans.push_back(temp);
return;
}
for (int i = 0; i < nums.size(); i++)
{
if (map[i] == 0)
{
map[i] = 1;
temp.push_back(nums[i]);
generate(nums, map, ans, temp);
map[i] = 0;
temp.pop_back();
}
}
}
vector<vector<int>> permute(vector<int> &nums)
{
vector<vector<int>> ans;
int n = nums.size();
vector<int> map(n, 0);
vector<int> temp;
generate(nums, map, ans, temp);
return ans;
}
};