-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain1.java
More file actions
31 lines (26 loc) · 817 Bytes
/
Main1.java
File metadata and controls
31 lines (26 loc) · 817 Bytes
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
import org.junit.Test;
import java.util.*;
/**
* Created by LXF on 2017/8/12.
*/
public class Main1 {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums);
backtrack(list, new ArrayList<>(), nums, 0);
return list;
}
void backtrack(List<List<Integer>> list , List<Integer> tempList, int [] nums, int start) {
list.add(new ArrayList<>(tempList));
for (int i = start; i < nums.length; i++) {
tempList.add(nums[i]);
backtrack(list, tempList, nums, i + 1);
tempList.remove(tempList.size() - 1);
}
}
@Test
public void test() {
List<List<Integer>> result = subsets(new int[]{1, 2, 3, 4});
System.out.println(result);
}
}