File tree Expand file tree Collapse file tree
src/main/java/leetcode/_78_ Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 )
You can’t perform that action at this time.
0 commit comments