Skip to content

Commit 568b120

Browse files
committed
Added :Subsets
1 parent 324fbf7 commit 568b120

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Given a set of distinct integers, nums, return all possible subsets (the power set).
2+
3+
Note: The solution set must not contain duplicate subsets.
4+
5+
Example:
6+
7+
Input: nums = [1,2,3]
8+
Output:
9+
[
10+
[3],
11+
[1],
12+
[2],
13+
[1,2,3],
14+
[1,3],
15+
[2,3],
16+
[1,2],
17+
[]
18+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
3+
@lc id : 78
4+
@problem : Subsets
5+
@author : github.com/rohitkumar-rk
6+
@url : https://leetcode.com/problems/subsets/
7+
@difficulty : medium
8+
*/
9+
10+
class Solution {
11+
12+
public void subsetsHelper(int currIndex, int nums[], ArrayList<Integer> currSubset,
13+
List<List<Integer>> subsets){
14+
15+
subsets.add(new ArrayList<>(currSubset));
16+
17+
for(int i = currIndex; i < nums.length; i++){
18+
currSubset.add(nums[i]);
19+
subsetsHelper(i+1, nums, currSubset, subsets);
20+
currSubset.remove(currSubset.size() - 1);
21+
}
22+
23+
}
24+
25+
public List<List<Integer>> subsets(int[] nums) {
26+
List<List<Integer>> subsets = new ArrayList<>();
27+
subsetsHelper(0, nums, new ArrayList(), subsets);
28+
return subsets;
29+
}
30+
}

0 commit comments

Comments
 (0)