Skip to content

Commit e335150

Browse files
committed
3
1 parent 85de096 commit e335150

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

PermutationsII/PermutationsII.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,33 @@ class Solution {
2929
return true;
3030
}
3131
};
32+
33+
// recursive solution
34+
class Solution {
35+
public:
36+
void helper(vector<vector<int>>& result, vector<int>& num, int x) {
37+
if (x == num.size()) {
38+
result.push_back(num);
39+
return;
40+
}
41+
helper(result, num, x + 1);
42+
map<int, bool> hash;
43+
hash[num[x]] = true;
44+
for (int i = x + 1; i < num.size(); i++) {
45+
if (hash[num[i]]) {
46+
continue;
47+
}
48+
hash[num[i]] = true;
49+
swap(num[x], num[i]);
50+
helper(result, num, x + 1);
51+
swap(num[x], num[i]);
52+
}
53+
}
54+
55+
vector<vector<int> > permuteUnique(vector<int> &num) {
56+
sort(num.begin(), num.end());
57+
vector<vector<int>> result;
58+
helper(result, num, 0);
59+
return move(result);
60+
}
61+
};

0 commit comments

Comments
 (0)