Skip to content

Commit 718c2c9

Browse files
committed
finish "78. Subsets". Fix #78
1 parent af8a7be commit 718c2c9

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,12 @@
393393
//|{leetcode_base_url}/combinations/[Combinations]
394394
//|{source_base_url}/Combinations.java[Java]
395395
//|Medium
396-
//
397-
//|78
398-
//|{leetcode_base_url}/subsets/[Subsets]
399-
//|{source_base_url}/Subsets.java[Java]
400-
//|Medium
401-
//
396+
397+
|78
398+
|{leetcode_base_url}/subsets/[Subsets]
399+
|{source_base_url}/Subsets.java[Java]
400+
|Medium
401+
402402
//|79
403403
//|{leetcode_base_url}/word-search/[Word Search]
404404
//|{source_base_url}/WordSearch.java[Java]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
/**
7+
* = 78. Subsets
8+
*
9+
* https://leetcode.com/problems/subsets/[Subsets - LeetCode]
10+
*
11+
* Given a set of *distinct* integers, nums, return all possible subsets (the power set).
12+
*
13+
* *Note:* The solution set must not contain duplicate subsets.
14+
*
15+
* .Example:
16+
* [source]
17+
* ----
18+
* Input: nums = [1,2,3]
19+
* Output:
20+
* [
21+
* [3],
22+
* [1],
23+
* [2],
24+
* [1,2,3],
25+
* [1,3],
26+
* [2,3],
27+
* [1,2],
28+
* []
29+
* ]
30+
* ----
31+
*
32+
* @author D瓜哥, https://www.diguage.com/
33+
* @since 2020-01-02 19:56
34+
*/
35+
public class Subsets {
36+
/**
37+
* Runtime: 0 ms, faster than 100.00% of Java online submissions for Subsets.
38+
*
39+
* Memory Usage: 37.1 MB, less than 99.18% of Java online submissions for Subsets.
40+
*/
41+
public List<List<Integer>> subsets(int[] nums) {
42+
List<List<Integer>> result = new ArrayList<>();
43+
result.add(new ArrayList<>());
44+
for (int num : nums) {
45+
List<List<Integer>> newSubsets = new ArrayList<>();
46+
for (List<Integer> current : result) {
47+
ArrayList<Integer> integers = new ArrayList<>(current);
48+
integers.add(num);
49+
newSubsets.add(integers);
50+
}
51+
result.addAll(newSubsets);
52+
}
53+
return result;
54+
}
55+
56+
/**
57+
* Runtime: 74 ms, faster than 5.06% of Java online submissions for Subsets.
58+
*
59+
* Memory Usage: 50.3 MB, less than 5.74% of Java online submissions for Subsets.
60+
*/
61+
public List<List<Integer>> subsetsLevel(int[] nums) {
62+
if (Objects.isNull(nums) || nums.length == 0) {
63+
return Collections.emptyList();
64+
}
65+
Set<Set<Integer>> subsets = new HashSet<>();
66+
subsets.add(Collections.emptySet());
67+
for (int i = 0; i < nums.length; i++) {
68+
ArrayList<Set<Integer>> list = new ArrayList<>(subsets);
69+
list.forEach(s -> {
70+
for (int num : nums) {
71+
Set<Integer> set = new TreeSet<>(s);
72+
set.add(num);
73+
subsets.add(set);
74+
}
75+
});
76+
}
77+
List<List<Integer>> result = subsets.stream()
78+
.map(ArrayList::new)
79+
.collect(Collectors.toList());
80+
return result;
81+
}
82+
83+
public static void main(String[] args) {
84+
Subsets solution = new Subsets();
85+
int[] a1 = {1, 2, 3};
86+
List<List<Integer>> r1 = solution.subsets(a1);
87+
System.out.println(r1);
88+
}
89+
}

0 commit comments

Comments
 (0)