幂集。编写一种方法,返回某集合的所有子集。集合中不包含重复的元素。
说明:解集不能包含重复的子集。
示例:
输入: nums = [1,2,3]
输出:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public List<List<Integer>> subsets(int[] nums) {
if (nums == null || nums.length == 0) {
return new ArrayList<>();
}
List<List<Integer>> result = new ArrayList<>();
getSubSets(result, nums, 0, new ArrayList<>());
return result;
}
private void getSubSets(List<List<Integer>> result, int[] nums, int index, List<Integer> curRes) {
if (index == nums.length) {
List<Integer> one = new ArrayList<>(curRes);
result.add(one);
return;
}
curRes.add(nums[index]);
getSubSets(result, nums, index + 1, curRes);
curRes.remove(curRes.size() - 1);
getSubSets(result, nums, index + 1, curRes);
}
}
//leetcode submit region end(Prohibit modification and deletion)