[LeetCode][Java] Subsets
题目:
Given a set of distinct integers, nums, 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 nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
题意:
给定一个由不同数字组成的数组nums,返回这个数组中的全部的子集。
注意:
1.子集中的元素必须是升序排列
2.终于的结果中不能包括反复的子集。
算法分析:
结合上一题《Combinations》的方法,将上一题作为子函数来使用。
AC代码:
<span style="font-family:Microsoft YaHei;font-size:12px;">public class Solution
{
public ArrayList<ArrayList<Integer>> subsets(int[] nums)
{
ArrayList<ArrayList<Integer>> fres = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> flist= new ArrayList<Integer>();
Arrays.sort(nums);
fres.add(flist);
for(int i=1;i<=nums.length;i++)
{
ArrayList<ArrayList<Integer>> sres = new ArrayList<ArrayList<Integer>>();
sres=combine(nums, i);
fres.addAll(sres);
}
return fres;
}
public static ArrayList<ArrayList<Integer>> combine(int nums[], int k)
{
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if(nums.length<=0 || nums.length<k)
return res;
helper(nums,k,0,new ArrayList<Integer>(), res);
return res;
}
private static void helper(int nums[], int k, int start, ArrayList<Integer> item, ArrayList<ArrayList<Integer>> res)
{
if(item.size()==k)
{
res.add(new ArrayList<Integer>(item));
return;
}
for(int i=start;i<nums.length;i++) // try each possibility number in current position
{
item.add(nums[i]);
helper(nums,k,i+1,item,res); // after selecting number for current position, process next position
item.remove(item.size()-1); // clear the current position to try next possible number
}
}
}</span>
[LeetCode][Java] Subsets的更多相关文章
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- [LeetCode] 90.Subsets II tag: backtracking
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- [leetcode]90. Subsets II数组子集(有重)
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- N-Queens II leetcode java
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- Java for LeetCode 078 Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- LeetCode 90. Subsets II (子集合之二)
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- LeetCode 78 Subsets (所有子集)
题目链接:https://leetcode.com/problems/subsets/#/description 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
随机推荐
- Postman 安装及使用入门教程 | 前后台 写接口的 徐工给的
https://www.cnblogs.com/mafly/p/postman.html
- 必知干货:Web前端应用十种常用技术你全都知道吗?
Web前端应用十种常用技术,随着JS与XHTML的应用普及,越来越多的web界面应用技术出现在网站上,比如我们常见的日历控件,搜索下拉框等,这些web界面应用技术大大的丰富了网站的表现形式,本文将为您 ...
- error C2143: 语法错误 : 缺少“;”(在“&”的前面)
报错: error C2143: 语法错误 : 缺少“;”(在“&”的前面) 代码: #include <iostream> ostream & << (ost ...
- mosquitto linux部署
1:官网下载 https://mosquitto.org/files/source/ 本文使用的是mosquitto-1.5.tar.gz 2:解压mosquitto-1.5.tar.gz tar - ...
- python的2种字符串格式化输出
字符串格式化代码(typecode) 法一: %格式使用下面的格式 %[(name)] [flags] [width][.precision] typecode (name)输出字典的value使用, ...
- css 实践记录
子绝父相 https://developer.mozilla.org/zh-CN/docs/Web/CSS/position 利用子绝父相来实现一种比较老的居中方式:1.明确宽度:2.定位左边到容器的 ...
- Java:post请求
文章来源:https://www.cnblogs.com/hello-tl/p/9140870.html 0.post请求返回json import java.io.BufferedInputStre ...
- 给Django中的url起名字
url反转 =>reverse 1.from django.shortcuts import reverse 2. 利用reverse函数对URL名称进行反转 reverse(url名称 ...
- Centos7 中Nginx的安装与配置
安装与配置 1.安装nginx yum intsall nginxsudo systemctl start nginx 启动服务sudo firewall-cmd --permanent --zone ...
- Python+selenium(多表单、多窗口切换)
多表单切换 案例:在Frame.html文件种定位搜狗搜索页面,进行搜索操作 Frame.html <html> <head> <title>Frame_test& ...