【LeetCode】 Subsets
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],
[]
]
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
res.add(new ArrayList<Integer>());
if(S.length ==0)
return res;
Arrays.sort(S);
ArrayList<Integer> tmp = new ArrayList<Integer>();
for(int i = 1; i <= S.length; i++)
{
tmp.clear();
recursion(res,tmp,i,S,0);
}
return res;
}
public void recursion(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> tmp, int k, int[] S, int dp)
{
if(k == tmp.size())
{
res.add(new ArrayList<Integer>(tmp));
return ;
}
for(int i = dp; i < S.length; i++)
{
tmp.add(S[i]);
recursion(res,tmp,k,S,i+1);
tmp.remove(tmp.size() - 1);
}
}
}【LeetCode】 Subsets的更多相关文章
- 【leetcode】Subsets (Medium) ☆
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 【leetcode】Subsets II (middle) ☆
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 【leetcode】Subsets II
Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...
- 【leetcode】Subsets
Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...
- 【LeetCode】90. Subsets II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...
- 【leetcode】698. Partition to K Equal Sum Subsets
题目如下: 解题思路:本题是[leetcode]473. Matchsticks to Square的姊妹篇,唯一的区别是[leetcode]473. Matchsticks to Square指定了 ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- POJ 1456 (贪心+并查集) Supermarket
有n件商品,每件商品有它的利润和售出的最后期限,问能够得到的最大利润是多少 这道题和 HDU 1789 Doing Homework again 几乎一模一样,只不过这个是求最的扣分,本题是求最大利润 ...
- Uploadify上传Excel到数据库
前两章简单的介绍了Uploadify上传插件的基本使用和相关的属性说明.这一章结合Uploadify+ssh框架+jquery实现Excel上传并保存到数据库. 以前写的这篇文章 Jq ...
- oraclede chuangjian yu dajian(zhuan)
http://wenku.baidu.com/link?url=pIKLZJ4sAurjNGjwgChqjRMhCXfn77qy1K_EW3nlGn4eN4roDN8mhSG0GakYbrTBcsD4 ...
- impersonate a user
// This sample demonstrates the use of the WindowsIdentity class to impersonate a user. // IMPORTANT ...
- 如何拷贝CMD命令行文本到粘贴板
/********************************************************************* * 如何拷贝CMD命令行文本到粘贴板 * To copy ...
- python练习程序(c100经典例3)
题目: 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? for i in range(1,100000): a=i+100; b=a+168; sa=int ...
- java UncaughtExceptionHandler 处理线程意外中止
本文转自:http://peirenlei.iteye.com/blog/305079 Thread的run方法是不抛出任何检查型异常(checked exception)的,但是它自身却可能因为一个 ...
- 输出图片的php代码前面不能有空白行
第一行增加一个空白行,就至少会输出一个换行符在图片数据流的前面而图片是按图片流提供的信息显示的,前面多了内容就无法解析了.
- Php 笔记4-----php 细节知识
从 php5开始 php.ini register_globals参数为OFF ,禁止全局变量. 以前的情况下, 全局变量是默认为On的 , 所以,浏览器的表单中控件,会自动根据name在服务 ...
- 顺序表的基本操作(C)
在顺序存储结构实现基本操作:初始化.创建.插入.删除.查找.遍历.逆置.合并运算. 运行示例: 请输入线性表La的长度: 请输入线性表La中的元素(共5个) *** 此时线性表La中的元素 *** * ...