-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowerset.java
More file actions
26 lines (23 loc) · 785 Bytes
/
powerset.java
File metadata and controls
26 lines (23 loc) · 785 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
import java.util.List;
import java.util.ArrayList;
public class powerset {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
findSubsets(nums,list,new ArrayList<>(),0);
return list;
}
public static void findSubsets(int[] nums,List<List<Integer>> list,ArrayList<Integer> temp,int i){
if(i>=nums.length){
list.add(new ArrayList<>(temp));
return;
}
findSubsets(nums,list,temp,i+1);
temp.add(nums[i]);
findSubsets(nums,list,temp,i+1);
temp.remove(temp.size()-1);
}
public static void main(String[] args) {
int nums[]= {3,1,2};
System.out.println(subsets(nums));
}
}