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

Note:The solution set must not contain duplicate subsets.

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(nums == null || nums.length == 0)
            return res;

        List<Integer> line = new ArrayList<Integer>();
        for(int i = 0; i <= nums.length; i++) {
            helper(res, line, nums, 0, i);
        }

        return res;
    }

    public void helper(List<List<Integer>> res, List<Integer> line, int[] nums, int start, int size) {
        if(line.size() == size) {
            res.add(new ArrayList<Integer>(line));
            return;
        }

        for(int i = start; i < nums.length; i++) {
            if(i > 0 && nums[i] == nums[i - 1])
                continue;

            line.add(nums[i]);
            helper(res, line, nums, i + 1, size);
            line.remove(line.size() - 1);
        }
    }
}

results matching ""

    No results matching ""