-
Notifications
You must be signed in to change notification settings - Fork 0
/
0046.全排列.cpp
69 lines (65 loc) · 1.46 KB
/
0046.全排列.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
* @lc app=leetcode.cn id=46 lang=cpp
*
* [46] 全排列
*
* https://leetcode-cn.com/problems/permutations/description/
*
* algorithms
* Medium (71.25%)
* Likes: 448
* Dislikes: 0
* Total Accepted: 58.9K
* Total Submissions: 81K
* Testcase Example: '[1,2,3]'
*
* 给定一个没有重复数字的序列,返回其所有可能的全排列。
*
* 示例:
*
* 输入: [1,2,3]
* 输出:
* [
* [1,2,3],
* [1,3,2],
* [2,1,3],
* [2,3,1],
* [3,1,2],
* [3,2,1]
* ]
*
*/
// @lc code=start
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> permute(vector<int>& nums)
{
vector<vector<int>> results;
vector<int> res;
vector<bool> is_pop(nums.size(), false);
int pop_num = 0;
backtracking(nums, res, results, is_pop, pop_num);
return results;
}
void backtracking(vector<int> nums, vector<int> res, vector<vector<int>>& results, vector<bool> is_pop, int pop_num)
{
if (pop_num == nums.size()) {
results.push_back(res);
return;
}
bool skip;
for (int i = 0; i < nums.size(); ++i) {
if (is_pop[i]) {
continue;
}
is_pop[i] = true;
res.push_back(nums[i]);
backtracking(nums, res, results, is_pop, pop_num + 1);
is_pop[i] = false;
res.pop_back();
}
}
};
// @lc code=end