forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0047.cpp
43 lines (42 loc) · 1.05 KB
/
0047.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
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
vector<vector<int>> permuteUnique(vector<int>& nums)
{
if (nums.size() <= 1) return vector<vector<int>>{nums};
vector<vector<int>> result;
vector<int> tmp(nums.begin()+1, nums.end());
vector<vector<int>> perms = permuteUnique(tmp);
for (auto perm : perms)
{
for (int i = 0; i <= perm.size(); ++i)
{
auto p = perm;
p.insert(p.begin() + i, nums[0]);
result.push_back(p);
if (i < perm.size() and perm[i] == nums[0]) break;
}
}
return result;
}
};
int main()
{
vector<int> nums = {1, 1, 2};
auto result =Solution().permuteUnique(nums);
for (auto i : result)
{
for (auto j : i)
{
cout << j << " ";
}
cout << endl;
}
return 0;
}