Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
42 lines (31 loc) · 1.22 KB

File metadata and controls

42 lines (31 loc) · 1.22 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package Algorithms;
import java.util.ArrayList;
import java.util.Arrays;
public class SubSet2 {
public static void main(String[] args){
int[] a = {1,2,3};
ArrayList<ArrayList<Integer>> test = subsets(a);
}
public static ArrayList<ArrayList<Integer>> subsets(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num == null || num.length == 0) {
return result;
}
ArrayList<Integer> list = new ArrayList<Integer>();
Arrays.sort(num);
subsetsHelper(result, list, num, 0);
return result;
}
private static void subsetsHelper(ArrayList<ArrayList<Integer>> result,
ArrayList<Integer> list, int[] num, int pos) {
result.add(new ArrayList<Integer>(list));
System.out.printf("generate: %s pos:%d\n", list.toString(), pos);
for (int i = pos; i < num.length; i++) {
list.add(num[i]);
System.out.printf("ADD:%d\n", num[i]);
subsetsHelper(result, list, num, i + 1);
System.out.printf("REMOVE:%d i=%d\n", list.get(list.size() - 1),i);
list.remove(list.size() - 1);
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.