Skip to content

Commit f872f34

Browse files
committed
add iterate, add recursive with for loop method
1 parent f91a968 commit f872f34

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

code/LeetCode/src/backtracking/SubsetsII.java

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ public class SubsetsII {
1010
public static void main(String[] args) {
1111
int[] nums = {1,2,2};
1212
SubsetsII obj = new SubsetsII();
13-
List<List<Integer>> resultList = obj.subsetsWithDup(nums);
13+
//List<List<Integer>> resultList = obj.subsetsWithDup(nums);
14+
List<List<Integer>> resultList = obj.subsetsWithDupIterate(nums);
1415
System.out.println(Arrays.toString(resultList.toArray()));
1516
}
1617

1718
public List<List<Integer>> subsetsWithDup(int[] nums) {
1819
Arrays.sort(nums);
1920
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
2021
// dfs
21-
dfs(nums, resultList, new ArrayList<Integer>(), 0);
22+
//dfs(nums, resultList, new ArrayList<Integer>(), 0);
23+
dfsWithFor(nums, resultList, new ArrayList<Integer>(), 0);
24+
//subsetsWithDupHelper(nums, resultList, new ArrayList<Integer>(), 0);
2225

2326
return resultList;
2427
}
@@ -41,27 +44,50 @@ private void dfs(int[] nums, List<List<Integer>> resultList, List<Integer> list,
4144

4245
}
4346

44-
// still have some duplicate data
45-
//private void dfs1(int[] nums, List<List<Integer>> resultList, List<Integer> list, int start) {
46-
// // exit
47-
// if (start == nums.length) {
48-
// resultList.add(new ArrayList<>(list));
49-
// return;
50-
// }
51-
//
52-
// for (int i = start; i < nums.length; i++) {
53-
// // duplicate case
54-
// if (i > start && nums[i] == nums[i - 1]) {
55-
// continue;
56-
// }
57-
//
58-
// // pick up
59-
// list.add(nums[i]);
60-
// dfs(nums, resultList, list, i + 1);
61-
//
62-
// // not pick up
63-
// list.remove(list.size() - 1);
64-
// dfs(nums, resultList, list, i + 1);
65-
// }
66-
//}
47+
private void dfsWithFor(int[] nums, List<List<Integer>> resultList, List<Integer> list, int start) {
48+
// exit
49+
if (start <= nums.length) {
50+
resultList.add(new ArrayList<>(list));
51+
}
52+
53+
for (int i = start; i < nums.length; i++) {
54+
// duplicate case
55+
if (i > start && nums[i] == nums[i - 1]) {
56+
continue;
57+
}
58+
59+
// pick up
60+
list.add(nums[i]);
61+
dfsWithFor(nums, resultList, list, i + 1);
62+
63+
// not pick up
64+
list.remove(list.size() - 1);
65+
}
66+
}
67+
68+
public List<List<Integer>> subsetsWithDupIterate(int[] nums) {
69+
Arrays.sort(nums);
70+
List<List<Integer>> resultList = new ArrayList<List<Integer>>();
71+
List<Integer> list = new ArrayList<Integer>();
72+
resultList.add(list);
73+
74+
int duplicateStart = 0;
75+
for (int i = 0; i < nums.length; i++) {
76+
int begin = 0;
77+
int size = resultList.size();
78+
if (i > 0 && nums[i] == nums[i - 1]) {
79+
begin = duplicateStart;
80+
}
81+
82+
for (int k = begin; k < size; k++) {
83+
List<Integer> newList = new ArrayList<Integer>(resultList.get(k));
84+
newList.add(nums[i]);
85+
resultList.add(newList);
86+
}
87+
88+
duplicateStart = size;
89+
}
90+
91+
return resultList;
92+
}
6793
}

0 commit comments

Comments
 (0)