78. Subsets

来源:互联网 发布:linux jar 解压命令 编辑:程序博客网 时间:2024/06/08 18:59

Given a set of distinct integers, nums, return all possible subsets (the power set).

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],  []]
求子集,含空,思路同上一题77. Combinations。程序如下:

class Solution {    public List<List<Integer>> subsets(int[] nums) {         List<Integer> lst = new ArrayList<>();        List<List<Integer>> llst = new ArrayList<>();        backTracing(llst, lst, nums, 0, nums.length);        return llst;           }    public void backTracing(List<List<Integer>> llst, List<Integer> lst, int[] nums, int begin, int n){        if (begin > n){            return;        }        llst.add(new ArrayList<Integer>(lst));        for (int i = begin; i < n; ++ i){            lst.add(nums[i]);            backTracing(llst, lst, nums, i + 1, n);            lst.remove(lst.size() - 1);        }    }}



原创粉丝点击