Skip to content

Commit be693d8

Browse files
committed
0078. Subsets
1 parent 4b9bc79 commit be693d8

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed

markdown/0078. Subsets.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [78\. Subsets](https://leetcode.com/problems/subsets/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a set of **distinct** integers, _nums_, return all possible subsets (the power set).
7+
8+
**Note:** The solution set must not contain duplicate subsets.
9+
10+
**Example:**
11+
12+
```
13+
Input: nums = [1,2,3]
14+
Output:
15+
[
16+
[3],
17+
  [1],
18+
  [2],
19+
  [1,2,3],
20+
  [1,3],
21+
  [2,3],
22+
  [1,2],
23+
  []
24+
]
25+
```
26+
27+
28+
#### Solution
29+
30+
Language: **Java**
31+
32+
```java
33+
class Solution {
34+
   public List<List<Integer>> subsets(int[] nums) {
35+
       Arrays.sort(nums);
36+
       List<List<Integer>> result = new ArrayList<>();
37+
       List<Integer> item = new ArrayList<>();
38+
       result.add(item);
39+
       for (int subSetSize = 1; subSetSize <= nums.length; subSetSize++) { // 这里的 subSetSize 表示这次生成的是几个元素的 set
40+
           for (int x = 0; x < result.size(); x++) {
41+
               List<Integer> list = result.get(x);
42+
               if (subSetSize == list.size() + 1) { // 由于此次需要加入一个元素,所以需要将 size 为 subSetSize-1 的 list 拿出来处理
43+
                   for (int j = list.size(); j < nums.length; j++) {
44+
                       if ((list.size() == 0 || nums[j] > list.get(list.size() - 1))) { // 这里保证生成的 size 必须是升序的
45+
                           List<Integer> newItem = new ArrayList<>(list);
46+
                           newItem.add(nums[j]);
47+
                           result.add(newItem);
48+
                      }
49+
                  }
50+
              }
51+
          }
52+
      }
53+
       return result;
54+
  }
55+
}
56+
```
57+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190725231521.png)

src/main/java/leetcode/_78_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._78_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
int[] nums = {3, 1, 7};
10+
System.out.println(solution.subsets(nums));
11+
}
12+
}
13+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package leetcode._78_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
class Solution {
8+
public List<List<Integer>> subsets(int[] nums) {
9+
Arrays.sort(nums);
10+
List<List<Integer>> result = new ArrayList<>();
11+
List<Integer> item = new ArrayList<>();
12+
result.add(item);
13+
for (int subSetSize = 1; subSetSize <= nums.length; subSetSize++) { // 这里的 subSetSize 表示这次生成的是几个元素的 set
14+
for (int x = 0; x < result.size(); x++) {
15+
List<Integer> list = result.get(x);
16+
if (subSetSize == list.size() + 1) { // 由于此次需要加入一个元素,所以需要将 size 为 subSetSize-1 的 list 拿出来处理
17+
for (int j = list.size(); j < nums.length; j++) {
18+
if ((list.size() == 0 || nums[j] > list.get(list.size() - 1))) { // 这里保证生成的 size 必须是升序的
19+
List<Integer> newItem = new ArrayList<>(list);
20+
newItem.add(nums[j]);
21+
result.add(newItem);
22+
}
23+
}
24+
}
25+
}
26+
}
27+
return result;
28+
}
29+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [78\. Subsets](https://leetcode.com/problems/subsets/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given a set of **distinct** integers, _nums_, return all possible subsets (the power set).
7+
8+
**Note:** The solution set must not contain duplicate subsets.
9+
10+
**Example:**
11+
12+
```
13+
Input: nums = [1,2,3]
14+
Output:
15+
[
16+
[3],
17+
  [1],
18+
  [2],
19+
  [1,2,3],
20+
  [1,3],
21+
  [2,3],
22+
  [1,2],
23+
  []
24+
]
25+
```
26+
27+
28+
#### Solution
29+
30+
Language: **Java**
31+
32+
```java
33+
class Solution {
34+
   public List<List<Integer>> subsets(int[] nums) {
35+
       Arrays.sort(nums);
36+
       List<List<Integer>> result = new ArrayList<>();
37+
       List<Integer> item = new ArrayList<>();
38+
       result.add(item);
39+
       for (int subSetSize = 1; subSetSize <= nums.length; subSetSize++) { // 这里的 subSetSize 表示这次生成的是几个元素的 set
40+
           for (int x = 0; x < result.size(); x++) {
41+
               List<Integer> list = result.get(x);
42+
               if (subSetSize == list.size() + 1) { // 由于此次需要加入一个元素,所以需要将 size 为 subSetSize-1 的 list 拿出来处理
43+
                   for (int j = list.size(); j < nums.length; j++) {
44+
                       if ((list.size() == 0 || nums[j] > list.get(list.size() - 1))) { // 这里保证生成的 size 必须是升序的
45+
                           List<Integer> newItem = new ArrayList<>(list);
46+
                           newItem.add(nums[j]);
47+
                           result.add(newItem);
48+
                      }
49+
                  }
50+
              }
51+
          }
52+
      }
53+
       return result;
54+
  }
55+
}
56+
```
57+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190725231521.png)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy