-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47. Permutations II.cpp
31 lines (29 loc) · 1.03 KB
/
47. Permutations 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
class Solution {
public:
vector<int> temp;
void allpermuation( vector<int>& nums, vector<vector<int>> &res, vector<bool> &chosen )
{
if( temp.size() == nums.size() )
{
res.push_back(temp);
return;
}
for(int i=0; i <nums.size(); i++)
{
if( chosen[i] == true || ( i> 0 && nums[i-1] == nums[i] && /*if i-1 element has been chosen previsouly then we can choose the repeated element*/ chosen[i-1] == false ) ) // if already taken in any of previous loction do not take again
continue;
chosen[i] = true;
temp.push_back(nums[i]);
allpermuation(nums, res, chosen);
temp.pop_back();
chosen[i] = false;
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<bool> chosen(nums.size());
vector<vector<int>> res;
allpermuation(nums, res, chosen);
return res;
}
};