Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
这道题就是combination的基础上,把每个组合输出出来。不用限制组合长度。
10 / 10 test cases passed.
Status: Accepted
Runtime: 1 ms
Memory Usage: 37.6 MB
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
helper(nums, results, list, 0);
return results;
}
public void helper(int[] nums, List<List<Integer>> results, List<Integer> list,
int n){
results.add(new LinkedList<Integer>(list));
for(int i = n; i < nums.length; i++){
list.add(nums[i]);
helper(nums, results, list, i + 1);
list.remove(list.size() - 1);
}
}
}