栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java子集leetcode

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java子集leetcode

给你一个整数数组 nums ,数组中的元素 互不相同 。返回该数组所有可能的子集(幂集)。

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

提示:

1 <= nums.length <= 10
-10 <= nums[i] <= 10
nums 中的所有元素 互不相同

class Solution {

    List> res = new ArrayList<>();
    LinkedList path = new LinkedList<>();

    public List> subsets(int[] nums) {
        //res.add(new LinkedList<>());
        recuit(nums,0);
        return res;
    }

    public void recuit(int[] nums,int start){
        res.add(new LinkedList<>(path));
        for(int i = start;i < nums.length;i++){
            path.addLast(nums[i]);
            recuit(nums,i+1);
            path.removeLast();
        }
    }
}

1ms;41.8MB

把LinkedList替换为ArrayList,

class Solution {

    List> res = new ArrayList<>();
    ArrayList path = new ArrayList<>();

    public List> subsets(int[] nums) {
        //res.add(new LinkedList<>());
        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++){
            path.add(nums[i]);
            recuit(nums,i+1);
            path.remove(path.size()-1);
        }
    }
}

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

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821860.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号