Skip to content

Commit 4a8fdaa

Browse files
committed
Leetcode 78. Subsets
1 parent 4dd19b6 commit 4a8fdaa

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

Backtracking/Subsets.java

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
/**
4+
* Given a set of distinct integers, nums, return all possible subsets (the power set).
5+
*
6+
* Note: The solution set must not contain duplicate subsets.
7+
* Example:
8+
* Input: nums = [1,2,3]
9+
* Output:
10+
* [
11+
* [3],
12+
* [1],
13+
* [2],
14+
* [1,2,3],
15+
* [1,3],
16+
* [2,3],
17+
* [1,2],
18+
* []
19+
* ]
20+
*/
21+
22+
public class Subsets
23+
{
24+
public List<List<Integer>> findSubsets(int[] nums)
25+
{
26+
List<List<Integer>> result = new ArrayList<>();
27+
Arrays.sort(nums);
28+
29+
backtrack(result, new ArrayList<>(), nums, 0);
30+
return result;
31+
}
32+
33+
private void backtrack(List<List<Integer>> result, List<Integer> temp, int[] nums, int start)
34+
{
35+
result.add(new ArrayList<>(temp));
36+
System.out.println("start = " + start + " result currently -> " + result);
37+
38+
for(int i = start; i < nums.length; i++)
39+
{
40+
temp.add(nums[i]);
41+
System.out.println("i = " + i + " temp currently -> " + temp);
42+
System.out.println("Calling Backtrack recursive");
43+
backtrack(result, temp, nums, i + 1);
44+
45+
System.out.println("After recursion call. i = " + i);
46+
temp.remove(temp.size() - 1);
47+
System.out.println("Temp currently after removal : " + temp);
48+
49+
}
50+
}
51+
52+
public static void main(String[] args) {
53+
Subsets obj = new Subsets();
54+
55+
int[] nums = {2, 1, 3};
56+
57+
System.out.println("Final result : " + obj.findSubsets(nums));
58+
}
59+
}

LeetCode/78. Subsets/Solution.java

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Given a set of distinct integers, nums, return all possible subsets (the power set).
3+
* Note: The solution set must not contain duplicate subsets.
4+
*
5+
* Example:
6+
* Input: nums = [1,2,3]
7+
* Output:
8+
* [
9+
* [3],
10+
* [1],
11+
* [2],
12+
* [1,2,3],
13+
* [1,3],
14+
* [2,3],
15+
* [1,2],
16+
* []
17+
* ]
18+
*/
19+
20+
class Solution {
21+
public List<List<Integer>> subsets(int[] nums) {
22+
List<List<Integer>> result = new ArrayList<>();
23+
24+
Arrays.sort(nums);
25+
26+
backtrack(result, new ArrayList<>(), nums, 0);
27+
28+
return result;
29+
}
30+
31+
private void backtrack(List<List<Integer>> result, List<Integer> temp, int[] nums, int start)
32+
{
33+
result.add(new ArrayList<>(temp));
34+
35+
for(int i = start; i < nums.length; i++)
36+
{
37+
temp.add(nums[i]);
38+
39+
backtrack(result, temp, nums, i+1);
40+
41+
temp.remove(temp.size() - 1);
42+
}
43+
}
44+
}

LeetCode/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
- [x] [Merge Intervals](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/56.%20Merge%20Intervals)
2525
- [x] [Meeting Rooms II](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/253.%20Meeting%20Rooms%20II)
2626

27+
## Backtracking
28+
- [x] [Subsets]()
29+
2730
## Linked List
2831

2932
- [x] [Add Two Numbers](https://github.com/kalpak92/DataStructures_Algorithms/tree/master/LeetCode/2.%20Add%20Two%20Numbers)

0 commit comments

Comments
 (0)