78.子集

作者: 名字是乱打的 | 来源:发表于2021-08-15 19:41 被阅读0次

思路:

回溯法
这里要注意,由于我们同一集合不同顺序的结果算同一种结果,因此我们在考虑的时候要进行排序已有元素类型的情况

代码:

class Solution {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> res=new LinkedList<>();
        getAll(0,nums,new LinkedList<Integer>(),res);
        return res;
    }

    private void getAll(int i, int[] nums, LinkedList<Integer> curr, List<List<Integer>> res) {
        res.add(new LinkedList<>(curr));
        //排序第i和元素参与排序的情况,这样每次其实我们是排除了一种已有元素排序的情况
        for (int j = i; j < nums.length; j++) {
            if (!curr.contains(nums[j])){
                curr.add(nums[j]);
                getAll(j+1,nums,curr,res);
                curr.removeLast();
            }
        }
    }

}

相关文章

  • LeetCode-78-子集

    LeetCode-78-子集 78. 子集[https://leetcode-cn.com/problems/su...

  • LeetCodeDay53 —— 子集★★

    78. 子集 Subsets 描述 Given a set of distinct integers, nums,...

  • 回溯递归算法

    回溯大法严重依赖【递归】 1、求子集 78. 子集[https://leetcode-cn.com/problem...

  • 子集 + 子集 II AND 零花钱兑换 + 零钱兑换 II

    78. 子集[https://leetcode-cn.com/problems/subsets/] 方法一 枚举 ...

  • 78.子集

    题目给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 ...

  • 78.子集

    代码解答 思路解析 读题分析应该用递归,第一个数与剩余数组合,第二个数与排除第一个数后剩余数组合...到达边界后返...

  • 78. 子集

    题目描述 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子...

  • 78. 子集

    给定一组不含重复元素的整数数组nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例:...

  • 78. 子集

    很经典的回溯问题,每一次的递归结束后,return。会继续执行removelast语句先存起来,在此基础上实现子集的遍历

  • 78. 子集

    (不同的整数,返回所有可能的子集,离散数学中叫power set)

网友评论

      本文标题:78.子集

      本文链接:https://www.haomeiwen.com/subject/ueaubltx.html