Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
 
这个题目思路就是类似于DFS的backtracking.
 
1. Constraints
1) No duplicate subsets and neither integers in the nums.
2) edge case len(nums) == 0 => [ [ ] ]  但是还是可以
 
2. Ideas
DFS 的backtracking    T: O(2^n)     S; O(n)
 
3. Code 
 
1) using deepcopy, but the idea is obvious, use DFS [] -> [1] -> [1, 2] -> [1, 2, 3] then pop the last one, then keep trying until the end. 
import copy
class Solution:
def subsets(self, nums: 'List [int]') -> 'List [ List [int] ]' :
ans = []
def helper(nums, ans, temp, pos):
ans.append(copy.deepcopy(temp))
for i in range(pos, len(nums)):
temp.append()
helper(nums, ans, temp, i + 1)
temp.pop() helper(nums, ans, [], 0)
return ans
2) Use the same idea , but get rid of deepcopy
 
class Solution:
def subsets(self, nums: 'List[int]') -> 'List[List[int]]':
ans = []
def helper(nums, ans, temp, pos):
ans.append(temp)
for i in range(pos, len(nums)):
helper(nums, ans, temp + [nums[i]], i + 1)
helper(nums, ans, [], 0)
return ans
 
 
 

[LeetCode] 78. Subsets tag: backtracking的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  2. Leetcode 78. Subsets (backtracking) 90 subset

    using prev class Solution { List<List<Integer>> res = new ArrayList<List<Integer&g ...

  3. [LeetCode] 78. Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  4. LeetCode 78. Subsets(子集合)

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

  5. LeetCode 78 Subsets (所有子集)

    题目链接:https://leetcode.com/problems/subsets/#/description   给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...

  6. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  7. [leetcode]78. Subsets数组子集

    Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...

  8. leetCode 78.Subsets (子集) 解题思路和方法

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  9. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

随机推荐

  1. H - The Frog's Games

    The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. On ...

  2. python如何去掉字符串‘\xa0’

    发现问题: 最近在用爬虫爬取网页信息时,在网页源码中遇到了“&nbsp"字符串,经查阅,发现该字符是不间断空格符.我们通常所用的空格是 \x20 ,是在标准ASCII可见字符 0x2 ...

  3. 单目三维稠密重建方案:Quadtree-accelerated Real-time Monocular Dense Mapping

    论文:This is a monocular dense mapping system following the IEEE Robotics and Automation Letters (RA-L ...

  4. ubuntu 搜狗输入法 在中断失效

    实测是更换了皮肤后,出现在中断输入故障,乱码.在其他界面可能是正常的,更换语言没用. killall fcitx输入法突然好了(根据网友所说,更改一堆东西貌似并没有什么用) 此时关闭输入法皮肤一切正常 ...

  5. Elasticsearch全文检索实战小结

    一.项目概述 这是一个被我称之为“没有枪.没有炮,硬着头皮自己造”的项目.项目是和其它公司合作的三个核心模块开发. 使用ES的目的是: 1).采集数据.网站数据清洗后存入ES: 2).对外提供精确检索 ...

  6. event.stopPropagation(),event.preventDefault()和return false的区别

    event.stopPropagation(),event.preventDefault()和return false的区别 1.event.stopPropagation()方法 这是阻止事件的冒泡 ...

  7. [daily] socks代理转化为http代理

    我用SS爬梯子,它是socks5的代理,在电脑上. 很长时间以来,我的手机是不能出去的.那么我该怎么弄才能让手机也出去呢.最简单的办法是让手机也ss. 但问题是,怎么给手机装上一个ss. 1.  用电 ...

  8. [development][libconfig] 配置文件库

    以前,一直用ini的配置文件. 简单清晰但是不灵活. 换一个: 试试libconfig 主页:  http://www.hyperrealm.com/oss_libconfig.shtml githu ...

  9. js实现数字千分位

    /** * * @param num * @param precision * @param separator * @returns {*} *=========================== ...

  10. tp5, laravel, yii2我该选择哪个

    为什么写这篇文章 我个人有一个技术群,里面学什么框架的都有,经常会有人问 某某功能 在 哪个框架怎么实现,用什么框架实现更好,说道这里我大家讲一个同类型问题的笑话 某女:你能让这个论坛的人都吵起来,我 ...