LeetCode-78. Subsets
来源:互联网 发布:高考奇迹知乎 编辑:程序博客网 时间:2024/06/14 18:17
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],
[]
]
使用回溯法,具体可参考Combination Sum中的讲解。
针对这题我们需要求出给定集合的所有子集,所以当然也包括空集。以[1,2,3]为例,搜索的策略:
- 先把空集加入到返回链表;
- 再有下图的顺序搜索得到所有的解。
class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res=new LinkedList<List<Integer>>(); subsetsBacktrack(res,new LinkedList<Integer>(),nums,0); return res; } private void subsetsBacktrack(List<List<Integer>> subsetsList,LinkedList<Integer> tempList,int[] nums,int start) { subsetsList.add(new LinkedList<Integer>(tempList)); for(int i=start;i<nums.length;++i) { tempList.add(nums[i]); subsetsBacktrack(subsetsList,tempList,nums,i+1); tempList.remove(tempList.size()-1); } }}
阅读全文
0 0
- LeetCode 78. Subsets
- [LeetCode]78.Subsets
- LeetCode --- 78. Subsets
- [Leetcode] 78. Subsets
- [leetcode] 78.Subsets
- 【leetcode】78. Subsets
- Leetcode 78. Subsets
- 78. Subsets LeetCode
- [leetcode] 78. Subsets
- leetcode 78. Subsets
- Leetcode 78. Subsets
- LeetCode *** 78. Subsets
- LeetCode 78. Subsets
- LeetCode-78.Subsets
- leetcode 78. Subsets
- LeetCode - 78. Subsets
- [LeetCode] 78. Subsets
- 【题解】Leetcode.78.Subsets
- python 绘制nc格式源清单
- ASP.NET 添加日志记录功能
- 【设计模式】服务定位器模式(Service Locator Pattern)
- Spark应用的优化
- Arcgis for Android解决标注TextSymbol中文字体乱码的问题
- LeetCode-78. Subsets
- 【设计模式】享元模式(Flyweight Pattern)
- html5实现弹出提示框,并且伴随遮罩层并且可以关闭弹出框
- # FLume 自定义Channel使用中遇到的问题(1)
- c语言练习题(持续更新中)
- JavaWeb支付宝沙箱环境小记
- 欢迎使用CSDN-markdown编辑器
- 文章标题
- 大数据浪潮下,「管理会计」迎来新机遇