# [刷题]Subsets

[LintCode]Subsets

Version I

class Solution {
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> S) {
// 2015-07-07
ArrayList<ArrayList<Integer>> rst = new ArrayList<>();
if (S == null) {
return rst;
}
ArrayList<Integer> list = new ArrayList<>();

helper(rst, list, S);
return rst;
}

private void helper(ArrayList<ArrayList<Integer>> rst,
ArrayList<Integer>list, ArrayList<Integer> S) {
// 树的深度
// if (list.size() == S.size()) {
//     return;
// }

for (int i = 0; i < S.size(); i++) {
if (list.size() > 0 && list.get(list.size() - 1) >= S.get(i)) {
continue;
}
helper(rst, list, S);
list.remove(list.size() - 1);
}
return;
}
}


Version II

class Solution {
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> S) {
// 2015-08-28
// S中不含相同元素，每个元素只可以用一次
// 解集中不可以含相同解
// 因此 解中不会含相同元素
ArrayList<ArrayList<Integer>> rst = new ArrayList<>();
if (S == null || S.size() == 0) {
return rst;
}
Collections.sort(S);
ArrayList<Integer> list = new ArrayList<>();
helper(S, rst, list, 0);
return rst;

}

private void helper(ArrayList<Integer> S, ArrayList<ArrayList<Integer>> rst,
ArrayList<Integer> list, int pos) {
// if (pos == S.size()) {
//     return;
// }
for (int i = pos; i < S.size(); i++) {
helper(S, rst, list, i + 1);
list.remove(list.size() - 1);
}
}
}

#### LeetCode 78. Subsets 解题报告

2016-02-17 09:09:55

#### LeetCode OJ 系列之78 Subsets --Python

2015-11-18 22:07:50

#### [刷题]Subsets II

2015-07-08 09:49:31

#### LeetCode Subsets 和 LeetCode Subsets II

2014-04-09 22:02:48

#### 全子集问题（subset）

2016-10-10 16:30:23

#### [leetcode刷题系列]Subsets

2013-08-06 12:26:54

#### LeetCode刷题笔录Subsets

2014-07-21 08:10:50

#### leetcode 78. Subsets-数组子集|回溯算法

2016-06-07 17:04:45

#### LeetCode刷题（42）--Subsets

2017-11-19 17:19:02

#### LeetCode刷题：78. Subsets

2018-04-06 19:36:34