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,再练习一道类似题目。该题目同样可以使用DFS,先把集合里的数按升序排列好。递归截止条件为当前子集的长度与原集合的长度相等,即当前子集等于原集合。每次递归开始前都先将当前的子集加入到子集集合中,每次递归都将nums[i]加入到子集中,然后继续递归。

class Solution(object):
  def subsets(self, nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    A = []
    def dfs(start, length, List):
      A.append(List)
      if length == len(nums):
        return
      for i in range(start, len(nums)):
        dfs(i+1, length+1, List+[nums[i]])
    nums.sort()
    dfs(0, 0, [])
    return A

时间: 2024-10-10 07:41:02

LeetCode 78. Subsets 20170606的相关文章

[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 (子集) 解题思路和方法

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 (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;

[leetcode]90. Subsets II数组子集(有重)

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 题意: 是的[leetcode

[LeetCode] 90.Subsets II tag: backtracking

Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Example: Input: [1,2,2] Output: [ [2], [1], [1,2,2], [2,2], [1,2], [] ] 这个题目思路

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

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

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 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[

LeetCode --- 90. Subsets II

题目链接: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]