-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleetcode_47.cpp
More file actions
36 lines (31 loc) · 1.24 KB
/
Copy pathleetcode_47.cpp
File metadata and controls
36 lines (31 loc) · 1.24 KB
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
class Solution {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> res;
vector<int> current;
vector<bool> used(nums.size(), false);
backtrack(nums, current, used, res);
return res;
}
private:
void backtrack(const vector<int>& nums, vector<int>& current,
vector<bool>& used, vector<vector<int>>& res) {
if (current.size() == nums.size()) {
res.push_back(current);
return;
}
// 각 재귀 깊이에서 이미 처리한 숫자들을 기록하는 로컬 컨테이너
unordered_set<int> seen;
for (int i = 0; i < nums.size(); i++) {
if (used[i]) continue;
// 현재 깊이에서 같은 숫자를 이미 사용했다면 건너뜀
if (seen.count(nums[i])) continue;
seen.insert(nums[i]);
used[i] = true;
current.push_back(nums[i]);
backtrack(nums, current, used, res);
current.pop_back();
used[i] = false;
}
}
};