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. 客户端TortoiseSVN的安装及使用方法 (申明:来源于网络)

    客户端TortoiseSVN的安装及使用方法 (申明:来源于网络) 地址:http://blog.chinaunix.net/uid-27004869-id-4112057.html

  2. Android开发实战(申明:来源于网络)

    Android开发实战(申明:来源于网络) 地址:http://so.csdn.net/so/search/s.do?q=Android%E5%BC%80%E5%8F%91%E5%AE%9E%E6%8 ...

  3. 泡泡一分钟:Towards real-time unsupervised monocular depth estimation on CPU

    Towards real-time unsupervised monocular depth estimation on CPU Matteo Poggi , Filippo Aleotti , Fa ...

  4. .net webservice的get支持,

    默认创建的webservices.asmx是不支持get的, 如 [WebMethod] public string HelloWorld() { return "Hello World&q ...

  5. [No0000F4]C# 枚举(Enum)

    枚举是一组命名整型常量.枚举类型是使用 enum 关键字声明的. C# 枚举是值数据类型.换句话说,枚举包含自己的值,且不能继承或传递继承. 声明 enum 变量 声明枚举的一般语法: enum &l ...

  6. Deck of Cards ZOJ - 2852 dp 多决策 三维 滚动更新

    题意:一个特殊21点游戏 具体http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2852 题解:建一个三维dp,表示三个卡槽分别 ...

  7. jdbc实现分页,需要前端传当前页码

    1.封装一个公共实体类用于返回:实体数据,当前页,总页数,总条数,每页多少条 public class PageInfo<T> { //一页显示的记录数 private int numPe ...

  8. 使用double无法得到数学上的精确结果的原因及为何不能用double来初始化BigDecimal

    使用double无法得到数学上的精确结果的原因: double类型的数值占用64bit,即64个二进制数,除去最高位表示正负符号的位,在最低位上一定会与实际数据存在误差(除非实际数据恰好是2的n次方) ...

  9. 终于碰到iOS对象集合深拷贝的坑

    从原始数组,拆分排列组合成新数组,同时给新的数组中的模型元素追加字段,数组的容量翻倍,如果不用深拷贝,后面追加的值就把前面的值覆盖了 UnitModel *model1 = [UnitModel ne ...

  10. Shell脚本交互之:自动输入密码

    Shell脚本交互之:自动输入密码 2016年04月09日 19:41:26 zhangjikuan 阅读数:58208 标签: Shell交互自动输入密码expect重定向管道 更多 个人分类: A ...