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. 现在越来越喜欢用ajax传值了,这样能让网站的体验性很好,今天就总结了一下常用的

    这是不用循环的方法 就是传过来的是一位数组 //编辑党建分类 function gk_bj(id){ $.post("{:U('Luser/lei_edlt')}",{id:id} ...

  2. zabbix监控告警Received empty response from Zabbix Agent Assuming that agent dropped connection

    zabbix监控告警Received empty response from Zabbix Agent Assuming that agent dropped connection错误 查看zabbi ...

  3. 洛谷P1074 靶形数独【dfs】【剪枝】

    题目:https://www.luogu.org/problemnew/show/P1074 题意: 数独的分数如下.一个数独的总分数就是权值乘所填数字之和. 现在给一个未完成的数独,问分数最高的数独 ...

  4. .NET Core 中依赖注入 AutoMapper 小记

    最近在 review 代码时发现同事没有像其他项目那样使用 AutoMapper.Mapper.Initialize() 静态方法配置映射,而是使用了依赖注入 IMapper 接口的方式 servic ...

  5. React Router 用法

    React Router 用法 一.DEMO import React from "react"; import { HashRouter as Router, Route, Li ...

  6. RHEL5.5的安装文档

    ---恢复内容开始--- 1.1 安装操作系统 (1) 按“ENTER”键,进入图形模式安装: (2) 检测CD,点击“Skip”跳过: (3) 安装界面显示,点击“Next”: (4) 选择语言为“ ...

  7. kibana设置mapping

    demo: PUT linewell_assets_mgt_fz_es/lw_devices/_mapping { "properties": { "update_tim ...

  8. linux 系统中 /etc/passwd 和 /etc/shadow文件详解

    链接地址:http://blog.csdn.net/yaofeino1/article/details/54616440

  9. AES加解密所遇问题

    AES加解密后解密数据末尾携带多余空格,经查看是由于加密时数据不足16个字节自动补齐导致 解决办法:记录加密数据长度,解密后根据数据长度读取解密数据. 另外加密数据中可能存在0等数据,所以拷贝内容时最 ...

  10. sql-server数据库常用语句

    查看所有数据库名 select name from master..Sysdatabases order by name; 查看当前数据所有表 select name from sysobjects ...