LintCode 子集

题目

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

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

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

LintCode 子集_第1张图片
Paste_Image.png

代码

class Solution {
    /**
     * @param S: A set of numbers.
     * @return: A list of lists. All valid subsets.
     */
    public ArrayList> subsets(int[] num) {
        ArrayList> res = new ArrayList<>();
        
        if(num == null || num.length == 0)
            return res;
        
        ArrayList list = new ArrayList<>();
        
        Arrays.sort(num);
        
        helper(res, list, num, 0);
        return res;
    }

    private void helper(ArrayList> res, ArrayList list, int[] num, int pos) {
        
        res.add(new ArrayList<>(list));
        
        for(int i=pos;i

你可能感兴趣的:(LintCode 子集)