-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathPermutations II.cpp
47 lines (39 loc) · 1.03 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* Leet Code */
/* Title - Permutations II */
/* Created By - Akash Modak */
/* Date - 03/06/2023 */
// Given a collection of numbers, nums, that might contain duplicates, return all possible unique permutations in any order.
// Example 1:
// Input: nums = [1,1,2]
// Output:
// [[1,1,2],
// [1,2,1],
// [2,1,1]]
// Example 2:
// Input: nums = [1,2,3]
// Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
class Solution {
public:
void swap(vector<int> &nums, int i, int j) {
int t=nums[i];
nums[i]=nums[j];
nums[j]=t;
}
void perm(vector<int> nums, int i, set<vector<int>> &s){
if(i==nums.size()){
s.insert(nums);
return;
}
for(int j=i;j<nums.size();j++){
swap(nums, i,j);
perm(nums, i+1,s);
swap(nums,i,j);
}
}
vector<vector<int>> permuteUnique(vector<int>& nums) {
set<vector<int>> st;
perm(nums,0,st);
vector<vector<int>> res(st.begin(),st.end());
return res;
}
};