0
点赞
收藏
分享

微信扫一扫

Java子集2 leetcode

修炼之士 2022-04-22 阅读 56
java算法

给你一个整数数组 nums ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。

解集 不能 包含重复的子集。返回的解集中,子集可以按 任意顺序 排列。

提示:

1 <= nums.length <= 10
-10 <= nums[i] <= 10

class Solution {

    List<List<Integer>> res = new ArrayList<>();
    ArrayList<Integer> path = new ArrayList<>();
    
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        Arrays.sort(nums);
        recuit(nums,0);
        return res;      
    }
    public void recuit(int[] nums,int start){
        res.add(new ArrayList<>(path));
        for(int i = start;i < nums.length;i++){
            if(i > start && nums[i] == nums[i-1]){
                continue;
            }
            path.add(nums[i]);

            recuit(nums,i+1);

            path.remove(path.size() - 1);
        }
    }
}

执行用时:1 ms, 在所有 Java 提交中击败了99.25%的用户

内存消耗:41.4 MB, 在所有 Java 提交中击败了66.15%的用户

举报

相关推荐

0 条评论