-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSubsets.java
More file actions
54 lines (45 loc) · 1.25 KB
/
Copy pathSubsets.java
File metadata and controls
54 lines (45 loc) · 1.25 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
49
50
51
52
53
54
/**
* Notes
*
* Create a list of list
* List<List<Integer>> foo = new ArrayList<>();
*
* Print an integer array for debugging
* System.out.println(Arrays.toString(array));
*
* Print an ArrayList for debugging
* System.out.println(list.toString())
*
* Test Cases:
* [9,0,3,5,7]
* [1,2,3]
*
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
//number of subsets
int number_of_subsets = (int) Math.pow(2, nums.length);
List<List<Integer>> output = new ArrayList<>();
output.add(new ArrayList<>());
for (int i = 1; i < number_of_subsets; i++) {
int cur = i;
int index = 0;
int[] indices = new int[nums.length];
// convert decimal to binary
while (cur > 0){
if (cur % 2 != 0) {
indices[index] = 1;
}
index++;
cur = cur / 2;
}
List<Integer> interOutput = new ArrayList<>();
for (int j = indices.length - 1; j >= 0; j--) {
if (indices[j] == 1)
interOutput.add(nums[nums.length - j - 1]);
}
output.add(interOutput);
}
return output;
}
}