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

版权声明

本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部