[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>

版权声明:本文为博主原创文章,转载注明出处

时间: 2024-08-24 13:24:42

[LeetCode][Java] Subsets的相关文章

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, 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,2], a soluti

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

Subset II leetcode java

题目: Given a collection of integers that might contain duplicates, 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,2], a solution

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, 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,2]

Spiral Matrix leetcode java

题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example, Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 题解: 这道题是实现题. 考虑2个初始

Pascal&#39;s Triangle II Leetcode java

题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3,3,1]. Note: Could you optimize your algorithm to use only O(k) extra space? 题解: 为了达到O(k)的空间复杂度要求,那么就要从右向左生成结果.相当于你提前把上一行的计算出来,当前行就可以用上一次计算出的结果计算了

Spiral Matrix II leetcode java

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:这道题跟Spiral Matrix想法也是类似的,就是依照矩阵从外圈到内圈建立

Pascal&#39;s Triangle leetcode java(杨辉三角)

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题解:既然讲到了Pascal‘s Triangle,即杨辉三角.那么就先去Wikipedia上面复习一下杨辉三角吧:”杨辉三角形,又称賈憲三角形.帕斯卡三角形.海亚姆三角形,是二项式係數在的