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 ...
随机推荐
- NodeJS缓存机制:畅销货,就多囤一点呗
上一篇文章,我们已经实现了客户端向NodeJS服务器发出请求时,服务器从磁盘读取文件内容后,向客户端返回文件的数据.而对于爱莲(iLinkIT)的1对n的场景,即将文件共享出来之后,让多个用户同时下载 ...
- 鼠标按键自定义软件-X-Mouse Button Control
转载说明 本篇文章可能已经更新,最新文章请转:http://www.sollyu.com/mouse-button-x-mouse-button-custom-software-control/ 说明 ...
- TFS遇到TF14446错误的解决方法
先上图 使用TFS,之前遇到文件被删除直接获取最新版本就行了,今天遇到这个异常:[TF14446: 无法签出“$/****/****/**/Models.pdb”以进行编辑.您的客户端或团队项目配置为 ...
- 利器: 用Siege做Web服务器压测
用「Web压测」关键词检索,能找到好多进行压测的工具,比如ab.Http_load.Webbench.Siege这些,不过今天并不是要对这些工具做对比,毕竟我们只是想得到一个结果.本文主要介绍Sieg ...
- MAC机中安装RUBY环境
在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY的环境进行一总结.安装Ruby环境首先需要安装Xcode然后需要安装Homebrew,接下来需要安装RVM最后安装Ruby环境 ...
- 一款兼容pc 移动端的tab切换
利用touchslider.js插件来制作的tab切换,可任意修改很方便~~~ 样式: <style> .box-163css{ width:100%; position:relative ...
- linux笔记1
在root下创建用户 1.useradd abc //添加一个新用户 2. cat /etc/passwd //查看新用户是否存在 3.passwd abc 输入密码 (123456) ...
- php获取客户端ip地址
本文介绍一个,php获取客户端的IP地址的实例代码,有需要的朋友参考下吧. 获取客户端IP地址的代码,如下: 复制代码代码示例: <?php//取得客户端IP的函数function get_cl ...
- Linux各发行版本 优缺点 简介
2008.01.21 13:43 Linux最早由Linus Benedict Torvalds在1991年开始编写.在这之前,RichardStallman创建了Free SoftwareFound ...
- XE5 ANDROID平台 调用 webservice
服务端需要midas.dll XE5对android的平台支持很有吸引力,虽然目前用来直接开发应用到安卓市场卖赚钱可能性估计不大(安卓市场目前国内好像都是免费的天下),但是对于企业应用很是很有帮助 ...