Subset leetcode java
题目:
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],
[]
] 题解:
一个思路就是套用combination的方法,其实combination那道题就是在求不同n下的subset,这里其实是要求一个集合罢了。
例如k=3,n=1,用combination那道题的方法求得集合是[[1], [2], [3]];
k=3, n=2, 用combination那道题的方法求得集合是[[1, 2], [1, 3], [2, 3]]
k=3, n=3, 用combination那道题的方法求得集合是[[1,2,3]]
所以上述3个集合外加一个空集不就是
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
么?
只需要在combination的外面加个循环即可。 代码如下:
1 public static void dfs(int[] S, int start, int len, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
2 if(item.size()==len){
3 res.add(new ArrayList<Integer>(item));
4 return;
5 }
6 for(int i=start; i<S.length;i++){
7 item.add(S[i]);
8 dfs(S, i+1, len, item, res);
9 item.remove(item.size()-1);
}
}
public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> item = new ArrayList<Integer>();
if(S.length==0||S==null)
return res;
Arrays.sort(S);
for(int len = 1; len<= S.length; len++)
dfs(S,0,len,item,res);
res.add(new ArrayList<Integer>());
return res;
}
Reference:http://blog.csdn.net/worldwindjp/article/details/23300545 底下是另外一个很精炼的算法。
1 public static void dfs(int[] S, int start, ArrayList<Integer> item,ArrayList<ArrayList<Integer>> res){
2 for(int i=start; i<S.length;i++){
3 item.add(S[i]);
4 res.add(new ArrayList<Integer>(item));
5 dfs(S,i+1, item,res);
6 item.remove(item.size()-1);
7 }
8
9 }
public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> item = new ArrayList<Integer>();
if(S.length==0||S==null)
return res;
Arrays.sort(S);
dfs(S,0,item,res);
res.add(new ArrayList<Integer>());
return res;
}
Reference:http://blog.csdn.net/u011095253/article/details/9158397
Subset leetcode java的更多相关文章
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- Subset II leetcode java
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
- Regular Expression Matching leetcode java
题目: Implement regular expression matching with support for '.' and '*'. '.' Matches any single chara ...
- Sqrt(int x) leetcode java
Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735 题目: Implement int sqrt(int x). Co ...
- ZigZag Conversion leetcode java
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- [LeetCode][Java]Candy@LeetCode
Candy There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- [Leetcode][JAVA] Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- 【目录】LeetCode Java实现
这里记录一下自己刷的LeetCode题目. 有些博客用英文阐述自己的思路和收获,相当于练习一下英文的表达能力. 比较好的题目有加粗. 1. Two Sum 3. Longest Substring W ...
- Single Number II leetcode java
问题描述: Given an array of integers, every element appears three times except for one. Find that single ...
随机推荐
- 客户端javascript笔记
html 中的 onclick访问的是全局作用域
- Windows VC++常见问题汇总
1.warning C4996: 'setmode': The POSIX name for this item is deprecated. Instead, use the ISO C++ con ...
- Android 源码编译及常见错误及解决方法
最近要往arm开发板上移植android系统,大大小小的问题遇到了太多太多,都是泪啊.本人初接触嵌入式开发,对问题的根源不是太了解,不过好在每解决一个问题,便记录一下.话不多说,正式罗列问题: hos ...
- mysql---用户管理
#创建用户king , 登陆密码为1234 create user 'king' identified by '1234'; #查看创建用户的语句,即上面那条创建用户的语句 show grants f ...
- [CSS]下拉菜单
原理:先让下拉菜单隐藏,鼠标移到的时候在显示出来 1>display 无动画效果,图片是秒出 2>opacity 有动画效果,我这里是1S出现,推荐配合绝对定位使用
- xtraScrollableControl 滚动条随鼠标滚动
代码如下 // using System; using System.Windows.Forms; using DevExpress.XtraEditors; namespace WindowsFor ...
- PHP中使用Luhn算法校验信用卡及借记卡卡号
Luhn算法会通过校验码对一串数字进行验证,校验码通常会被加到这串数字的末尾处,从而得到一个完整的身份识别码. 我们以数字“7992739871”为例,计算其校验位: 从校验位开始,从右往左,偶数位乘 ...
- 使用jQuery的9个误区
千万别忘记了使用最新的版本哦,毕竟每个版本更新肯定会在功能或性能上有所提升,或者修复了几个Bug,但有时惰性让人不想再去研究新版本的变化,因此,提醒你别忘记了在新项目用新的一定比旧版本要好. AD: ...
- cc2640-各DEMO板性能分析
一.测试方法: 将4种模块同时上电,测量每个模块达到的最远距离.以稳定能建立通讯为连接上依据.4种板子分析为 1号阿莫DEMO板,2号咱们自己DEMO板,3号嘉源电子DEMO,4号陆程电子DEMO 全 ...
- php-fpm.conf 文件详解
pid string PID文件的位置. 默认为空. error_log string 错误日志的位置. 默认: 安装路径#INSTALL_PREFIX#/log/php-fpm.log. log_l ...