forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78. Subsets.txt
More file actions
26 lines (23 loc) · 760 Bytes
/
78. Subsets.txt
File metadata and controls
26 lines (23 loc) · 760 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
Leetcode - 78. Subsets
https://leetcode.com/problems/subsets/
import java.util.Arrays;
class Solution {
public List<List<Integer>> subsets(int[] n) {
Set<List<Integer>> arr= new HashSet<List<Integer>>();
List<Integer> ar=new LinkedList<Integer>();
traversal(0,n,arr,ar);
List<List<Integer>> arrr=new LinkedList<List<Integer>>();
arrr.addAll(arr);
return arrr;
}
public void traversal(int a,int[] n, Set<List<Integer>> arr,List<Integer> ar){
if(a==n.length){
arr.add(new LinkedList<>(ar));
return;
}
ar.add(n[a]);
traversal(a+1, n,arr,ar);
ar.remove(ar.size()-1);
traversal(a+1, n,arr,ar);
}
}