leetCode 78.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],
  []
]

思路:这题和上题的组合差不多,只是k的数字是变动的,从0-n。所以对比上一题,也就是对K加个循环。

代码如下:

public class Solution {
    boolean[] f;
    List<List<Integer>> list;
    public List<List<Integer>> subsets(int[] nums) {
        list = new ArrayList<List<Integer>>();

        if(nums.length == 0){//必须是有效k值
            return list;
        }
        f = new boolean[nums.length];

        for(int i = 0; i <= nums.length;i++){
        	count(nums,"",i,0);//调用函数
        }

        return list;
    }
    /**
     * 实现对k个数字的排练组合
     * @param a 数组
     * @param s 排练组合得到的结果
     * @param nn 排练组合的数字个数
     */
    private void count(int[] a,String s,int nn,int j){
        if(nn == 0){//处理结果
            String[] sa = s.split(",");//分割数组
            List<Integer> al = new ArrayList<Integer>();
            for(int i = 0;i < sa.length; i++){
                if(sa[i].length() > 0)//只有当不为空的时候才添加
                	al.add(Integer.parseInt(sa[i]));//添加
            }
            Collections.sort(al);//排序,非降序
            list.add(al);
            return;
        }
        //遍历,从i=j开始,只要i开头的与i大的值
        for(int i = j; i < a.length; i++){
            if(!f[i]){
                f[i] = true;
                count(a,s + "," + a[i],nn-1,i+1);//调用下一个为false的数字
                f[i] = false;
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 11:46:49

leetCode 78.Subsets (子集) 解题思路和方法的相关文章

[LeetCode] Subsets I (78) &amp; II (90) 解题思路,即全组合算法

78. 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]

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

【LeetCode】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], a solutio

[LeetCode] Longest Valid Parentheses 解题思路

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

LeetCode 78. Subsets 20170606

Given a set of distinct integers, nums, return all possible subsets. Note: 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], [] ] 题目大意:给定一个整数集合,返回其所有子集的集合

[LeetCode] 53. Maximum Subarray 解题思路

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 问题: 给定一个元素有正有负的数组,求最大连续子数组的和. 思路

leetCode 90.Subsets II(子集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

leetCode 51.N-Queens (n皇后问题) 解题思路和方法

N-Queens The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configur