ID | Title | Difficulty | |
---|---|---|---|
Loading... |
78. Subsets
Medium
LeetCode
Array, Backtracking, Bit Manipulation
Problem
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Code
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
helper(res, nums, 0, new ArrayList<>());
return res;
}
private void helper(List<List<Integer>> res, int[] nums, int index, List<Integer> temp){
res.add(new ArrayList<Integer>(temp));
for(int i = index; i < nums.length; i++){
temp.add(nums[i]);
helper(res, nums, i + 1, temp);
temp.remove(temp.size() - 1);
}
}
}
按 <- 键看上一题!
77. Combinations
按 -> 键看下一题!
79. Word Search