-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path1.java
More file actions
23 lines (20 loc) · 684 Bytes
/
1.java
File metadata and controls
23 lines (20 loc) · 684 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public List<List<Integer>> subsets(int[] nums) {
Deque<Integer> current = new ArrayDeque<>(nums.length);
List<List<Integer>> res = new ArrayList<>();
backtrack(nums, 0, current, res);
return res;
}
void backtrack(int[] nums, int k, Deque<Integer> current, List<List<Integer>> res) {
if (k == nums.length) {
res.add(new ArrayList<>(current));
return;
}
// 不选择第 k 个元素
backtrack(nums, k+1, current, res);
// 选择第 k 个元素
current.addLast(nums[k]);
backtrack(nums, k+1, current, res);
current.removeLast();
}
}