-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0047.全排列-ii.cpp
70 lines (66 loc) · 1.52 KB
/
0047.全排列-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
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
70
/*
* @lc app=leetcode.cn id=47 lang=cpp
*
* [47] 全排列 II
*
* https://leetcode-cn.com/problems/permutations-ii/description/
*
* algorithms
* Medium (53.73%)
* Likes: 181
* Dislikes: 0
* Total Accepted: 28.7K
* Total Submissions: 51.8K
* Testcase Example: '[1,1,2]'
*
* 给定一个可包含重复数字的序列,返回所有不重复的全排列。
*
* 示例:
*
* 输入: [1,1,2]
* 输出:
* [
* [1,1,2],
* [1,2,1],
* [2,1,1]
* ]
*
*/
// @lc code=start
#include <algorithm>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums)
{
vector<vector<int>> results;
vector<int> res;
vector<bool> used(nums.size(), false);
int n_used;
sort(nums.begin(), nums.end());
aux(nums, res, results, used, n_used);
return results;
}
void aux(vector<int>& nums, vector<int> res, vector<vector<int>>& results, vector<bool>& used, int n_used)
{
if (n_used == nums.size()) {
results.push_back(res);
return;
}
for (int i = 0; i < nums.size(); ++i) {
if (used[i]) {
continue;
}
used[i] = true;
res.push_back(nums[i]);
aux(nums, res, results, used, n_used + 1);
used[i] = false;
res.pop_back();
while (i < nums.size() - 1 && nums[i + 1] == nums[i]) {
++i;
}
}
}
};
// @lc code=end