-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathsubsetArray.java
More file actions
28 lines (26 loc) · 876 Bytes
/
subsetArray.java
File metadata and controls
28 lines (26 loc) · 876 Bytes
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
import java.util.*;
public class solution {
public static void printSubsets(int input[]) {
List<List<Integer>> subsets = subsets(input);
Object[] objArray = subsets.toArray();
for(int index=0;index<objArray.length;index++){
System.out.println(objArray[index]);
}
}
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
subsetsHelper(list, new ArrayList<>(), nums, 0);
return list;
}
public static void subsetsHelper(List<List<Integer>> list , List<Integer> resultList, int [] nums, int start){
list.add(new ArrayList<>(resultList));
for(int i = start; i < nums.length; i++){
// add element
resultList.add(nums[i]);
// Explore
subsetsHelper(list, resultList, nums, i + 1);
// remove
resultList.remove(resultList.size() - 1);
}
}
}