Skip to content

Commit 5e60d76

Browse files
committed
subsets add iterate, backtrack, binary sorted
1 parent 1eb63ee commit 5e60d76

File tree

1 file changed

+71
-1
lines changed

1 file changed

+71
-1
lines changed

code/LeetCode/src/backtracking/Subsets.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package backtracking;
22

3+
import javax.annotation.Resource;
34
import java.util.ArrayList;
45
import java.util.Arrays;
56
import java.util.List;
@@ -10,7 +11,10 @@ public class Subsets {
1011
public static void main(String[] args) {
1112
int[] nums = new int[]{1,2,3};
1213
Subsets obj = new Subsets();
13-
List<List<Integer>> resultList = obj.subsets(nums);
14+
//List<List<Integer>> resultList = obj.subsets(nums);
15+
//List<List<Integer>> resultList = obj.subsetsWithRecursion(nums);
16+
//List<List<Integer>> resultList = obj.subsetsWithBacktrack(nums);
17+
List<List<Integer>> resultList = obj.subsetsWithBinarySorted(nums);
1418
System.out.println(Arrays.toString(resultList.toArray()));
1519
}
1620

@@ -37,4 +41,70 @@ private void dfs(int[] nums, List<List<Integer>> resultList, List<Integer> list,
3741
list.remove(list.size() - 1);
3842
dfs(nums, resultList, list, index + 1);
3943
}
44+
45+
46+
public List<List<Integer>> subsetsWithRecursion(int[] nums) {
47+
List<List<Integer>> outputList = new ArrayList<List<Integer>>();
48+
outputList.add(new ArrayList<Integer>());
49+
for (int num: nums) {
50+
List<List<Integer>> newList = new ArrayList<List<Integer>>();
51+
for (List<Integer> list: outputList) {
52+
newList.add(new ArrayList<Integer>(list) {{ add(num); }});
53+
}
54+
55+
for (List<Integer> list: newList) {
56+
outputList.add(list);
57+
}
58+
}
59+
60+
return outputList;
61+
}
62+
63+
public List<List<Integer>> subsetsWithBacktrack(int[] nums) {
64+
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
65+
for (int len = 0; len <= nums.length; len++) {
66+
// backtrack
67+
backtrack(nums, resultList, new ArrayList<Integer>(), 0, len);
68+
}
69+
70+
return resultList;
71+
}
72+
73+
private void backtrack(int[] nums, List<List<Integer>> resultList, List<Integer> list, int first, int len) {
74+
// exit
75+
if (list.size() == len) {
76+
resultList.add(new ArrayList<Integer>(list));
77+
return;
78+
}
79+
80+
if (first == nums.length) {
81+
return;
82+
}
83+
list.add(nums[first]);
84+
backtrack(nums, resultList, list, first + 1, len);
85+
list.remove(list.size() - 1);
86+
backtrack(nums, resultList, list, first + 1, len);
87+
}
88+
89+
public List<List<Integer>> subsetsWithBinarySorted(int[] nums) {
90+
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
91+
int n = nums.length;
92+
93+
for (int i = (int)Math.pow(2, n); i < (int)Math.pow(2, n + 1); i++) {
94+
// generate bitmask, from 0..00 to 1..11
95+
String bitmask = Integer.toBinaryString(i).substring(1);
96+
97+
// append subset corresponding to that bitmask
98+
List<Integer> list = new ArrayList<Integer>();
99+
for (int k = 0; k < n; k++) {
100+
if (bitmask.charAt(k) == '1') {
101+
list.add(nums[k]);
102+
}
103+
}
104+
105+
resultList.add(list);
106+
}
107+
108+
return resultList;
109+
}
40110
}

0 commit comments

Comments
 (0)