【LeetCode】Subsets 解题报告
来源:互联网 发布:手机怎么ping网络 编辑:程序博客网 时间:2024/04/30 06:42
【题目】
Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
【解析】
题意:求一个集合的所有子集合,包括空集合和它本身。
与组合生成算法 【LeetCode】Combinations 解题报告类似。
先排好序,从前往后取相应数目的元素。
【回溯法】
public class Solution { int[] S = {}; int len = 0; List<List<Integer>> ans = new ArrayList<List<Integer>>(); public List<List<Integer>> subsets(int[] S) { this.S = S; this.len = S.length; Arrays.sort(S); // length of subsets: from 0 to S.length for (int i = 0; i <= len; i++) { backTracking(new ArrayList<Integer>(), 0, i); } return ans; } public void backTracking(List<Integer> list, int from, int tar) { if (list.size() == tar) { List<Integer> res = new ArrayList<Integer>(list); ans.add(res); } else { for (int i = from; i < len; i++) { list.add(S[i]); backTracking(list, i + 1, tar); list.remove(new Integer(S[i])); } } }}
0 0
- 【LeetCode】Subsets 解题报告
- LeetCode-Subsets-解题报告
- [LeetCode] Subsets 解题报告
- 【LeetCode】Subsets II 解题报告
- [leetcode] 78. Subsets 解题报告
- [LeetCode] Subsets II 解题报告
- LeetCode 78. Subsets 解题报告
- [Leetcode] 78. Subsets 解题报告
- LeetCode 78. Subsets 解题报告
- [leetcode] 90. Subsets II 解题报告
- Leetcode 78. Subsets 子集 解题报告
- [Leetcode] 90. Subsets II 解题报告
- Leetcode 90. Subsets II 子集2 解题报告
- 【LeetCode】698.Partition to K Equal Sum Subsets(Medium)解题报告
- LeetCode 解题报告索引
- [LeetCode]Permutations,解题报告
- Leetcode Triangle 解题报告
- [LeetCode]Anagrams,解题报告
- Hibernate与JDBC的优缺点
- springMVC3学习(二)--ModelAndView对象
- WinCE 5.0 内核启动过程
- MyEclipse开启Tomcat服务器时总是进入Debug模式的解决
- JavaScript中清空数组的三种方式
- 【LeetCode】Subsets 解题报告
- 深入理解log4j好文
- JQuery获取元素文档大小、偏移和位置和滚动条位置的方法集合
- 项目开发感言
- springMVC3学习(三)--handlerMapping和handlerAdapter
- 关于几个有用js
- 我希望在软件开发生涯初期就知道的 4 件事
- 180+简单测试用例 checklist
- 今天有人和我说pdo没办法show databases,实测可以