Subsets

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

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], [] ]

public class Solution {
    public List> subsets(int[] nums) {
        List> result = new ArrayList>();
        List list = new ArrayList();
        Arrays.sort(nums);
        helper(result, list, nums, 0);
        return result;
    }
    private void helper(List> result, List list, int[] nums, int index) {
        result.add(new ArrayList(list));

        for (int i = index; i < nums.length; i++) {
            if (list.contains(nums[i])) {
                continue;
            }
            list.add(nums[i]);
            helper(result,list,nums,i + 1);
            list.remove(list.size() - 1);
        }
    }
}

关键字:nums, list, subsets, result