-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations-ii.cc
31 lines (27 loc) · 946 Bytes
/
permutations-ii.cc
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
#include "leetcode.h"
using namespace std;
using namespace utils;
class Solution {
public:
static vector<vector<int>> permute_unique(vector<int> &numbers) {
std::sort(numbers.begin(), numbers.end());
vector<vector<int>> res;
permute_unique(numbers, 0, res);
return res;
}
static void permute_unique(vector<int> &numbers, const size_t hierarchy, vector<vector<int>> &res) {
if (hierarchy + 1 == numbers.size()) {
res.push_back(numbers);
} else {
for (size_t i = hierarchy; i < numbers.size(); ++i) {
if (i != hierarchy && numbers[i] == numbers[hierarchy]) { continue; }
std::swap(numbers[i], numbers[hierarchy]);
permute_unique(numbers, i + 1, res);
}
}
}
};
int main(int argc, char const *argv[]) {
vector<int> v{2, 2, 1, 1};
// cout << Solution::permute_unique(v) << endl;
}