题目:

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. postman使用--添加headers、授权、cookies

    添加headers Request Headers(请求头)用来说明服务器要使用的附加信息,比较重要的信息有:Cookie,Referer,User-Agent等.在postman中可以在请求下方的H ...

  2. 1、C编程预备计算机知识

    一.数据类型 基本数据类型 1.整数 整形 -- int -- 4 短整型 -- short int -- 2 长整型 -- long int --8 2.浮点数(实数) 单精度浮点数 -- floa ...

  3. openjdk-alpine镜像无法打印线程堆栈和内存堆栈问题

    基于openjdk:8u171-alpine构建的java镜像,使用jstack命令打印线程的时候会提示以下错误: /opt # ps -ef PID USER TIME COMMAND 1 root ...

  4. 年华利率n%

    年化利率12%指的是,在您出借的本金不减少的情况下,您一年后的利息将达到您出借本金的12%.也就是说,如果年化利率是12%,则每月您出借资金获得的利息是1%(12% / 12个月). 在有利网,您的投 ...

  5. C语言学习13

    快速排序 //快速排序 #include <stdio.h> void quicksort(int a[], int left, int right); void main() { ] = ...

  6. Android发布apk后解决百度地图不显示的问题

    今天在做Android apk发布时,发现发布后不能显示百度地图,结合网上的信息,及自己的实验,终于可以了.原来在Eclipse上直接run的是测试版本,而发布后是运行版本,两个版本的SHA1值不一样 ...

  7. XHR2:js异步上传

    http://dev.opera.com/articles/xhr2/ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  8. 可以从CSS框架中借鉴到什么

    http://isux.tencent.com/css-framework.html http://isux.tencent.com/css-framework.html 现在很多人会使用 CSS 框 ...

  9. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  10. RPC实现的底层原理及应用

    摘要:RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...