-
Notifications
You must be signed in to change notification settings - Fork 1
/
47.全排列-ii.cpp
40 lines (36 loc) · 959 Bytes
/
47.全排列-ii.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
/*
* @lc app=leetcode.cn id=47 lang=cpp
*
* [47] 全排列 II
*/
// @lc code=start
class Solution {
public:
vector<vector<int>> res;
vector<int> path;
vector<bool> st;
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
path = vector<int>(nums.size());
st = vector<bool>(nums.size());
dfs(nums, 0);
return res;
}
void dfs(vector<int> &nums, int u) {
if (u == nums.size()) {
res.push_back(path);
return;
}
for (int i = 0; i < nums.size(); i++) {
if (!st[i]) {
if (i && !st[i - 1] && nums[i - 1] == nums[i])
continue; // 剪枝,前一个用过了并且前一个与当前相同跳过
st[i] = 1;
path[u] = nums[i];
dfs(nums, u + 1);
st[i] = 0;
}
}
}
};
// @lc code=end