Subsets I
来源:互联网 发布:wish黄钻产品怎么优化 编辑:程序博客网 时间:2024/05/16 15:23
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], []]
Analysis: DFS. Sort the array at the beginning. Each time, select the first element in the array, which is the smallest number in the array and the biggest number compared to all of the numbers in res, and insert itself and its combinations with all of the existing sets in res into res.
public class Solution { public void subsets(int[] S, ArrayList<ArrayList<Integer>> res) { if(S.length == 0) { res.add(new ArrayList<Integer>()); // empty set return; } int size = res.size(); for(int i=0; i<size; i++) { ArrayList<Integer> tem = new ArrayList<Integer>(res.get(i)); tem.add(S[0]); res.add(tem); } int[] nextS = Arrays.copyOfRange(S, 1, S.length); ArrayList<Integer> loc = new ArrayList<Integer>(); loc.add(S[0]); res.add(loc); subsets(nextS, res); } public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); Arrays.sort(S); subsets(S, res); return res; }}
0 0
- Subsets I
- 【LeetCode】Subsets I && II
- LeetCode:Subsets I
- Subsets I, II
- [Leetcode]Subsets I&II
- LeetCode - Subsets I && II
- LeetCode | Subsets I,II
- 78. Subsets I & II
- LeetCode:Subsets I II
- 78. Subsets I && 90. Subsets II
- leetcode -day31 Subsets I II
- LeetCode题解:Subsets I and II
- <LeetCode OJ> 78 / 90 Subsets (I / II)
- LeetCode 78, 90. Subsets i, ii
- [Leetcode] #78#90 Subsets I & II
- Subsets
- Subsets
- Subsets
- IE7 浏览器position:absolute消失不见的原因
- iOS安全攻防(六):使用class-dump-z分析支付宝app
- mysql分区功能详细介绍,以及实例
- 工具与学习计划
- c# + sql procedure 实现分页
- Subsets I
- sockaddr与sockaddr_in结构体简介
- AOJ 0118 Property Distribution 题解
- Android开发者必知的开发资源
- Ajax+jquery 的前后台交互学习
- R语言与机器学习学习笔记(分类算法)(1)K-近邻算法
- MD5加密 Base64加密和解密算法 java
- eclipse里找不到com.sun.image.codec.jpeg.JPEGCodecimport与JPEGImageEncoder包和 import sun.misc.BASE64...
- 约瑟夫问题