题目:

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的更多相关文章

  1. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  2. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  3. [leetcode]90. Subsets II数组子集(有重)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  4. N-Queens II leetcode java

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  5. [LeetCode] 90. Subsets II 子集合 II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

  6. Java for LeetCode 078 Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  7. LeetCode 90. Subsets II (子集合之二)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  8. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  9. LeetCode 78 Subsets (所有子集)

    题目链接:https://leetcode.com/problems/subsets/#/description   给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...

随机推荐

  1. vue 组件名和方法名 重名了,报function错误

    vue 组件名和方法名 重名了,报function错误

  2. Ubuntu18.04 NVIDIA显卡驱动 安装大全

    离线安装NVIDIA显卡驱动 费了一天的劲,走了好多的坑,最主要的原因是gcc版本的问题,一定要用最新版本的gcc!!! 1)官网下载显卡驱动 2)apt 下载gcc包及其依赖包,可用apt-cach ...

  3. linux(Ubuntu/Centos) iproute 路由IP地址等命令集合,查看端口链接

    原 linux(Ubuntu/Centos) iproute 路由IP地址等命令集合,查看端口链接 2017年03月20日 16:55:57 风来了- 阅读数:2291 标签: centoslinux ...

  4. 解决SimpleDateFormat线程安全问题

    package com.tanlu.user.util; import java.text.DateFormat; import java.text.ParseException; import ja ...

  5. h5 页面 禁止网页缩放

    //禁用双指缩放: document.documentElement.addEventListener('touchstart', function (event) { if (event.touch ...

  6. CSRF verification failed. Request aborted. 表单提交方法为POST时的报错

    本人所用Django版本为1.11,在设置请求方法为POST时,遇到标题中的错误,尝试了多种方法,最终通过下面的操作来修复: 在template文件中添加图中红框部分 接着,导入csrf_exempt ...

  7. python-基本运算符(解压缩-必考)

    基本运算符 算术运算符 x =10 y =20 print(x+y) 30 print(x-y) -10 print(x*y) 200 print(x/y) 0.5 print(x%y)#取余 10 ...

  8. 嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=15873837810484552531 中的15-23讲

    #coding=gbk#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=1587383 ...

  9. pdfjs viewer 开发小结

    此文已由作者吴家联授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 1. pdfjs库简介 PDF.js 是由Mozilla 主导推出的可以将PDF文件转换为H5页面进行展示的 ...

  10. 按Esc按钮关闭layer弹窗

    //按Esc关闭弹出框 $(document).ready(function () { }).keydown( function (e) { if (e.which === 27) {  layer. ...