-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path47.cpp
More file actions
86 lines (73 loc) · 1.91 KB
/
47.cpp
File metadata and controls
86 lines (73 loc) · 1.91 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Author : Accagain
// Date : 17/3/26
// Email : chenmaosen0@gmail.com
/***************************************************************************************
*
* Given a collection of numbers that might contain duplicates, return all possible unique permutations.
*
* For example, [1,1,2] have the following unique permutations:
* [
* [1,1,2],
* [1,2,1],
* [2,1,1]
* ]
*
* 做法:
* dfs, 枚举的时候以不同的值开始枚举,开始先排个序
* 时间复杂度:
*
*
****************************************************************************************/
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <vector>
#include <string>
#define INF 0x3fffffff
using namespace std;
class Solution {
public:
bool vis[12000];
void dfs(vector<int> nums, vector<vector<int>> &ans, int now, vector<int>hav)
{
if(now == nums.size())
{
// for(int i=0; i<hav.size(); i++)
// printf("%d ", hav[i]);
// printf("\n");
ans.push_back(hav);
return ;
}
int tmp = -INF;
for(int i=0; i<nums.size(); i++)
{
if(!vis[i] && nums[i] != tmp)
{
vis[i] = 1;
hav.push_back(nums[i]);
tmp = nums[i];
dfs(nums, ans, now+1, hav);
hav.pop_back();
vis[i] = 0;
}
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
memset(vis, 0, sizeof(vis));
vector<vector<int>> ans;
vector<int>hav;
sort(nums.begin(), nums.end());
dfs(nums, ans, 0, hav);
return ans;
}
};
int main() {
Solution *test = new Solution();
int data[] = {1, 1, 3};
vector<int> x(data, data + sizeof(data) / sizeof(data[0]));
test->permuteUnique(x);
return 0;
}
//
// Created by cms on 17/3/26.
//