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. python多行代码简化

    python中,可以把多行代码简化为一行,把for循环和if条件判断都集中到一行里来写,示例如下: >>> from nltk.corpus import stopwords > ...

  2. MVC 二级联动

    后台代码,获取数据如下: /// <summary> /// 获取省份 /// </summary> public JsonResult GetProvincelist() { ...

  3. Luogu 1494 - 小Z的袜子 - [莫队算法模板题][分块]

    题目链接:https://www.luogu.org/problemnew/show/P1494 题目描述 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天 ...

  4. C#配置.INI文件

    百度搜了一些资料,好多没给示例,只给了代码.让人看了直接懵逼,后来找了个靠谱的:http://www.jb51.net/article/118591.htm

  5. mysql 事务队列 写入 幂等性 重复写入

    mysql  事务队列   写入 幂等性  重复写入

  6. Linux下稀疏文件的存储方式

    写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文! 本博客全网唯一合法URL:ht ...

  7. [daily] 像tcpdump一样监听unix domain socket

    如题. 见: https://superuser.com/questions/484671/can-i-monitor-a-local-unix-domain-socket-like-tcpdump? ...

  8. jpa持久化对象四种状态

    自己理解,不完全正确,大致如下: 例:某实体类   Person(int id,string name,int age);   id 为主键. 新建:new Person(),  并且未给 id 赋值 ...

  9. day5_函数_文件读写_用一个函数来满足文件的读或者写_应用默认参数

    import json def op_file_tojson(filename,dic=None): #默认值参数,根据是否传dic字典来判断读还是写 if dic: #如果dic传了值,不是空的,则 ...

  10. LeetCode 1012 Complement of Base 10 Integer 解题报告

    题目要求 Every non-negative integer N has a binary representation.  For example, 5 can be represented as ...