-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPermutations_II.cpp
More file actions
28 lines (28 loc) · 887 Bytes
/
Permutations_II.cpp
File metadata and controls
28 lines (28 loc) · 887 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
class Solution {
public:
bool isSkipped(int idx, int i, vector<int> &num) {
for(int j = idx; j < i; ++j) {
if(num[j] == num[i])
return true;
}
return false;
}
void permuteUniqueUtils(int idx, vector<int> &num, vector<vector<int> > &result) {
if(idx == num.size() - 1) {
result.push_back(num);
return;
}
for(int i = idx; i < num.size(); ++i) {
if(isSkipped(idx, i, num)) continue;
swap(num[idx], num[i]);
permuteUniqueUtils(idx + 1, num, result);
swap(num[idx], num[i]);
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > result;
if(num.size() == 0) return result;
permuteUniqueUtils(0, num, result);
return result;
}
};