leetcode-78. Subsets

78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]



解法:

这题考的主要是回溯算法,对于回溯算法,我看了很多博客讲解也不是很理解。

这里找到了一位博主的讲解,是比较容易理解的:点击打开链接


对于代码的具体执行过程和如何转化的,我还是不太理解

如果有朋友看到这篇博客并且对回溯算法有比较好的理解,感谢赐教!!!!

还是附上solu:

public class Solution {  
    public List<List<Integer>> subsets(int[] nums) {  
        List<List<Integer>> res = new ArrayList<List<Integer>>();  
        List<Integer> temp = new ArrayList<Integer>();  
        dfs(res, temp, nums, 0);  
        return res;  
    }  
    private void dfs(List<List<Integer>> res, List<Integer> temp, int[] nums, int j) {  
        res.add(new ArrayList<Integer>(temp));  
        for(int i = j; i < nums.length; i++) {  
            temp.add(nums[i]);  //① 加入 nums[i]  
            dfs(res, temp, nums, i + 1);  //② 递归  
            temp.remove(temp.size() - 1);  //③ 移除 nums[i]  
        }  
    }  
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值