题目

子集 

给定一个含不同整数的集合,返回其所有的子集

样例

如果 S = [1,2,3],有如下的解:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
注意

子集中的元素排列必须是非降序的,解集必须不包含重复的子集

挑战

你可以同时用递归与非递归的方式解决么?

解题

根据上面求排列的思想很类似,还是深度优先遍历。由于输出每个子集需要升序,所以要先对数组进行排序。求出所以的子集,也就是求出所以的组合方式 + 空集

问题转化为求组合方式的问题

参考链接不仅要考虑起始位置,还需要考虑长度,这样才是组合 C(n,k),由于我只想到要考虑起始位置,而长度问题在程序中增加,一直没有解决问题

核心程序

    public void helper(int[] nums,int start,int len,
    ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
        if( list.size() == len){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i=start;i< nums.length;i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(nums,i+1,len,list,res);
            list.remove(list.size()-1);
            }
        
    }
class Solution {
    /**
     * @param S: A set of numbers.
     * @return: A list of lists. All valid subsets.
     */
    public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
        // write your code here
        ArrayList<Integer> list = new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(nums == null || nums.length ==0)
            return res;
        Arrays.sort(nums);
        
        res.add(list);
        for(int len = 1;len<= nums.length;len++){
            helper(nums,0,len,list,res);
            
        }
        return res;
    }
    public void helper(int[] nums,int start,int len,
    ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
        if( list.size() == len){
            res.add(new ArrayList<Integer>(list));
            return;
        }
        for(int i=start;i< nums.length;i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(nums,i+1,len,list,res);
            list.remove(list.size()-1);
            }
        
    }
}
Java Code

九章中程序进行了优化,长度不考虑,递归一次list的值都是子集的一个元素

class Solution {
    /**
     * @param S: A set of numbers.
     * @return: A list of lists. All valid subsets.
     */
    public ArrayList<ArrayList<Integer>> subsets(int[] nums) {
        // write your code here
        ArrayList<Integer> list = new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(nums == null || nums.length ==0)
            return res;
        Arrays.sort(nums);
        helper(nums,0,list,res);
     
        return res;
    }
    public void helper(int[] nums,int start, ArrayList<Integer> list,ArrayList<ArrayList<Integer>> res){
        res.add(new ArrayList<Integer>(list));
        for(int i=start;i< nums.length;i++){
            if(list.contains(nums[i])){
                continue;
            }
            list.add(nums[i]);
            helper(nums,i+1,list,res);
            list.remove(list.size()-1);
            }
        
    }
}
Java Code
class Solution:
    """
    @param S: The set of numbers.
    @return: A list of lists. See example.
    """
    def subsets(self, S):
        def dfs(depth, start, valuelist):
            res.append(valuelist)
            if depth == len(S): return
            for i in range(start, len(S)):
                dfs(depth+1, i+1, valuelist+[S[i]])
        S.sort()
        res = []
        dfs(0, 0, [])
        return res
Python Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-22
  • 2021-07-28
  • 2021-05-24
  • 2022-12-23
  • 2022-01-20
  • 2022-02-08
猜你喜欢
  • 2022-03-08
  • 2021-06-07
  • 2021-11-29
  • 2022-12-23
  • 2021-07-18
  • 2022-12-23
相关资源
相似解决方案