-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path47_permutation_ii
27 lines (27 loc) · 926 Bytes
/
47_permutation_ii
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
class Solution {
private:
void helper(vector<vector<int>>& result, vector<int> current, int start, int end, int step){
if (step == current.size()){
result.push_back(current);
return;
}
unordered_set<int> swapped;
for (int i = start; i < end; ++i){
if ((i == start || current[start] != current[i]) && swapped.count(current[i]) == 0){
swapped.insert(current[i]);
swap(current[start], current[i]);
helper(result, current, start + 1, end, step + 1);
swap(current[i], current[start]);
}
}
return;
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
vector<int> current = nums;
if (nums.empty()) return result;
helper(result, current, 0, nums.size(), 1);
return result;
}
};