Leetcode 78. Subsets (backtracking) 90 subset

using prev

class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> subsets(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        back(temp,nums, 0);
        return res;
    }
    void back(List<Integer> temp,int[] nums, int prev){
        List<Integer> list = new ArrayList<>(temp);
        //if(!res.contains(list))
        res.add(list);

        for(int i = prev; i<nums.length; i++){
            temp.add(nums[i]);
            back(temp, nums, i+1);
            temp.remove(temp.size()-1);
        }
    }
}

// 90 subset2

sorting the array first and check the contains

class Solution {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<Integer> temp = new ArrayList<>();
        Arrays.sort(nums);
        back(temp,nums, 0);
        return res;
    }
    void back(List<Integer> temp,int[] nums, int prev){
        List<Integer> list = new ArrayList<>(temp);
        if(!res.contains(list))
        res.add(list);

        for(int i = prev; i<nums.length; i++){
            temp.add(nums[i]);
            back(temp, nums, i+1);
            temp.remove(temp.size()-1);
        }
    }

}

原文地址:https://www.cnblogs.com/stiles/p/Leetcode78.html

时间: 2024-08-04 11:46:46

Leetcode 78. Subsets (backtracking) 90 subset的相关文章

leetCode 78.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

[LeetCode] 78. Subsets 子集合

Given a set of distinct integers, nums, return all possible subsets (the power set). Note: 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], [] ] Python:

LeetCode 78. Subsets 20170606

Given a set of distinct integers, nums, return all possible subsets. Note: 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], [] ] 题目大意:给定一个整数集合,返回其所有子集的集合

DFS解法的两道题 Leetcode 46 Permutations &amp; Leetcode 78 Subset

Leetcode 78 Subset Given a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1

【Leetcode】78. Subsets(求集合的子集问题)

78. Subsets(求集合的子集问题) [分析]:求集合的所有子集问题.题目要求子集中元素非递减序排列,因此我们先要对原来的集合进行排序.原集合中每一个元素在子集中有两种状态:要么存在.要么不存在.这样构造子集的过程中每个元素就有两种选择方法:选择.不选择,因此可以构造一颗二叉树,例如对于例子中给的集合[1,2,3],构造的二叉树如下(左子树表示选择该层处理的元素,右子树不选择),最后得到的叶子节点就是子集:{ 链接 } 1 class Solution 2 { 3 private: 4 v

Leetcode总结之Backtracking

本文我们就Leetcode中的一个类型的题目backtracking进行一系列的总结和归纳.backtracking这个方法本质是建立在递归的基础上,不断尝试新的路径,这里关键是每次尝试完以后需要退回来也就是回溯. 如果你已经找到了解决方案,那么返回成功 for(现在位置可以的所有可能选择){ 选择其中一个方案然后沿着路径前进一步 使用递归的方法从新的位置解决问题 如果新的位置可以成功解决,向上一级返回成功 从现在位置恢复到循环之前的位置 } 如果到这里表示仍然没有成功,返回失败 下面我们来看一

【LeetCode】Subsets II 解题报告

[题目] Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solutio

Java for LeetCode 090 Subsets II

Given a collection of integers that might contain duplicates, 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,2], a soluti

LeetCode OJ - Subsets 1 &amp;&amp; 2

这道题的做法,一定得掌握啊!!!  elegant & beautiful & concise 下面是AC代码: 1 /** 2 * Given a set of distinct integers, S, return all possible subsets. 3 * 这道题的做法应该要记住!!!!! 4 * @param s 5 * @return 6 */ 7 public ArrayList<ArrayList<Integer>> subsets(int[