给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
    说明:解集不能包含重复的子集。
    示例:
    输入: nums = [1,2,3]
    输出:
    [
    [3],
    [1],
    [2],
    [1,2,3],
    [1,3],
    [2,3],
    [1,2],
    []
    ]
    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/subsets


    思路:
    根据不同的子集的范围来遍历

    1. class Solution {
    2. public List<List<Integer>> subsets(int[] nums) {
    3. List<List<Integer>> res = new LinkedList<>();
    4. if (nums.length == 0){
    5. return res;
    6. }
    7. Deque<Integer> path = new ArrayDeque<Integer>();
    8. int len = nums.length;
    9. for (int size = 0; size <= nums.length; size++) {
    10. dfs(0,nums, path, res, size); // 不同的子集大小
    11. }
    12. return res;
    13. }
    14. private void dfs(int first,int[] nums, Deque<Integer> path, List<List<Integer>> res,int size) {
    15. if (path.size() == size) {
    16. res.add(new ArrayList<>(path));
    17. return;
    18. }
    19. for (int i = first; i < nums.length; i++) {
    20. path.add(nums[i]);
    21. dfs(i + 1,nums,path,res,size);
    22. path.remove(nums[i]);
    23. }
    24. }
    25. }