【第22期】观点:IT 行业加班,到底有没有价值?

LeetCode OJ 系列之78 Subsets --Python

原创 2015年11月18日 22:07:50

Problem:

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,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
Answer:

class Solution(object):
    
    def subsets(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = self.subset_func(nums)
        for line in result:
            line.sort()
        return result
        
    def subset_func(self,nums):
        if len(nums)==0:
            return [[]]
        if len(nums)==1:
            return [[],nums]
        first = nums[0]
        tmp_nums = nums[1:]
        tmp_result = self.subset_func(tmp_nums)
        result = []
        for key in tmp_result:
            result.append(key)
            tmp = list(key)
            tmp.append(first)
            result.append(tmp)
        return result
        

Analysis:

利用动态规划的思想,当想要为集合A添加一个新的元素x时,得到的新集合A+1,必然是由下面两部分组成:

  • 集合A里所有的项
  • 集合A里每一项添加新的元素x之后形成的新项。

版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

【算法】【动态规划】Subset Sum

问题描述:   When you divide numbers from 1 to N into two subsets, you can make each subset sum equal. F...
  • boywgw
  • boywgw
  • 2015-08-27 15:58
  • 1278

leetcode 78. Subsets-数组子集|回溯算法

原题链接:78. Subsets 【思路1-Java】回溯算法|递归实现 本解法采用回溯算法实现,回溯算法的基本形式是“递归+循环”,正因为循环中嵌套着递归,递归中包含循环,这才使得回溯比一般的递归和...

两种回溯方法解决子集问题的思路 leetcode 78. Subsets

leetcode 78. Subsets  注意:理解回溯算法尽量采用动态规划的思路。 第一种,例子{1,2,3} ,子集问题即为各位有无两种情况,那么就可以直接搜索下去。 im...

78. Subsets 详细解答

Recursive (Backtracking) This is a typical problem that can be tackled by backtracking. Since bac...

LeetCode 78 Subsets (Python详解及实现)

【题目】 Given a set of distinct integers, nums,return all possible subsets.   Note: The solution set...

[Leetcode] Subsets 1 ^& 2

<div id="question_78" class="row-even question-title" style="margin: 0px; padding: 5px 8px; border: 1px solid #ffffff; font-size: 13px; vertical-align: baseline; cursor: pointer; color: #222222; font-family: 'Helvetica Neue', arial,

leetcode 78. Subsets

Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must no...

[LeetCode]78.Subsets

题目 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 so

<LeetCode OJ> 78 / 90 Subsets (I / II)

Total Accepted: 87879 Total Submissions: 285264 Difficulty: Medium Given a set of distinct inte...

[LeetCode]90.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 exampl
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)