乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      LeetCode 78 [Subsets]

       雪柳花明 2016-10-07

      源網(wǎng)址:http://www.jianshu.com/p/ac57b9d3d211


      原題

      給定一個含不同整數(shù)的集合,返回其所有的子集

      如果 S = [1,2,3],有如下的解:

      [
        [3],
        [1],
        [2],
        [1,2,3],
        [1,3],
        [2,3],
        [1,2],
        []
      ]

      子集中的元素排列必須是非降序的,解集必須不包含重復(fù)的子集

      解題思路

      • Backtracking, DFS
      • 首先,數(shù)組要排序,在第n層,加入一個元素進入n+1層,刪除剛剛加入的元素,加入第n層的第二個元素......
                                         [ ]
                                      /   |                                [1]   [2]   [3]
                                 /  |     |
                          [1, 2] [1, 3] [2, 3]
                            /
                      [1, 2, 3]

      完整代碼

      # 方法一
      class Solution(object):
          def subsets(self, nums):
              """
              :type nums: List[int]
              :rtype: List[List[int]]
              """
              if nums is None:
                  return []
              res = [[]]
              self.dfs(sorted(nums), 0, [], res)
              return res
      
          def dfs(self, nums, index, path, res):
              for i in xrange(index, len(nums)):
                  res.append(path + [nums[i]])
                  path.append(nums[i])
                  self.dfs(nums, i+1, path, res)
                  path.pop()
      
      # 方法二
      class Solution(object):
          def subsets(self, nums):
              """
              :type nums: List[int]
              :rtype: List[List[int]]
              """
              if nums is None:
                  return []
              res = [[]]
              self.dfs(sorted(nums), 0, [], res)
              return res
      
          def dfs(self, nums, index, path, res):
              for i in xrange(index, len(nums)):
                  res.append(path + [nums[i]])
                  self.dfs(nums, i+1, path+[nums[i]], res)
      
      # 方法三
      class Solution(object):
          def subsets(self, nums):
              """
              :type nums: List[int]
              :rtype: List[List[int]]
              """
              nums.sort()
              result = [[]]
              for x in nums:
                  with_x = []
                  for s in result:
                      with_x.append(s + [x])
                  result = result + with_x
          return result

      如果覺得我的文章對您有用,請隨意打賞。您的支持將鼓勵我繼續(xù)創(chuàng)作!

      ¥ 打賞支持


      文/Jason_Yuan(簡書作者)
      原文鏈接:http://www.jianshu.com/p/ac57b9d3d211
      著作權(quán)歸作者所有,轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),并標(biāo)注“簡書作者”。

        本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多