-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutationii.cpp
More file actions
37 lines (36 loc) · 966 Bytes
/
Copy pathpermutationii.cpp
File metadata and controls
37 lines (36 loc) · 966 Bytes
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
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
if(nums.size() == 0) return res;
//sort(nums.begin(), nums.end());
vector<int> item;item.clear();
helper(nums, 0, item, res);
return res;
}
void helper(vector<int> nums, int index, vector<int>& item, vector<vector<int>>& res)
{
if(index == nums.size())
{
res.push_back(item);
return;
}
for(int i = index; i < nums.size(); i++)
{
if(i > index && find(nums, index, i)) continue;
swap(nums[index], nums[i]);
item.push_back(nums[index]);
helper(nums, index + 1, item, res);
item.pop_back();
swap(nums[index], nums[i]);
}
}
bool find(vector<int> nums, int start, int end)
{
for(int i = start; i < end; i++)
{
if(nums[i] == nums[end]) return true;
}
return false;
}
};