-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_0078SubSets.java
More file actions
48 lines (39 loc) · 1.29 KB
/
_0078SubSets.java
File metadata and controls
48 lines (39 loc) · 1.29 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
37
38
39
40
41
42
43
44
45
46
47
48
package com.heatwave.leetcode.problems;
import java.util.ArrayList;
import java.util.List;
public class _0078SubSets {
static class Solution {
List<Integer> temp = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
backtracking(nums, 0);
return ans;
}
private void backtracking(int[] nums, int index) {
ans.add(new ArrayList<>(temp));
for (int i = index; i < nums.length; i++) {
temp.add(nums[i]);
backtracking(nums, i + 1);
temp.remove(temp.size() - 1);
}
}
}
static class AnotherSolution {
List<Integer> temp = new ArrayList<>();
List<List<Integer>> ans = new ArrayList<>();
public List<List<Integer>> subsets(int[] nums) {
backtracking(nums, 0);
return ans;
}
private void backtracking(int[] nums, int index) {
if (index == nums.length) {
ans.add(new ArrayList<>(temp));
return;
}
temp.add(nums[index]);
backtracking(nums, index + 1);
temp.remove(temp.size() - 1);
backtracking(nums, index + 1);
}
}
}