给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
[3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

思路:回溯,在循环中嵌套递归,递归自然就有了循环的特性

Solution().subsets([1,2,3])

[[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
 class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = []
def recursive(start,num):
res.append(num)
for i in range(start,len(nums)):
recursive(i+1,num+[nums[i]])
recursive(0,[])
return res

两层for,每次让res中已有的子集添加当前的num形成新的子集

[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]
 class Solution:
def subsets(self, nums: List[int]) -> List[List[int]]:
res = [[]]
for num in nums:
for temp in res[:]:
t = temp[:]
t.append(num)
res.append(t)
return res

LeetCode--078--子集(python)的更多相关文章

  1. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  2. 每日一题-——LeetCode(78)子集

    给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3],  [1],  [2],  [1,2,3],  [1,3],  [2, ...

  3. [LeetCode]题解(python):078 Subsets

    题目来源 https://leetcode.com/problems/subsets/ Given a set of distinct integers, nums, return all possi ...

  4. [LeetCode]题解(python):090 Subsets II

    题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...

  5. [LeetCode]题解(python):125 Valid Palindrome

    题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...

  6. [LeetCode]题解(python):120 Triangle

    题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...

  7. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  8. [LeetCode]题解(python):118 Pascal's Triangle

    题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...

  9. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  10. [LeetCode]题解(python):114 Flatten Binary Tree to Linked List

    题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...

随机推荐

  1. 自定义标记mark

    前言 pytest可以支持自定义标记,自定义标记可以把一个web项目划分多个模块,然后指定模块名称执行.app自动化的时候,如果想android和ios公用一套代码时,也可以使用标记功能,标明哪些是i ...

  2. 使用Postman对HTTP接口进行功能测试

    一.工具说明 Postman是一种网页调试与发送网页http请求的工具.我们可以用来很方便的模拟get或者post或者其他方式的请求来调试接口. 二.应用场景 1.Get请求 get请求通过接口参数拼 ...

  3. win7自带录像工具怎么打开?win7自带录像工具的使用方法

    http://www.xitongcheng.com/jiaocheng/win7_article_28327.html 制作教程的好帮手 win7自带录像工具怎么打开?win7自带录像工具的使用方法 ...

  4. 正则表达式——推荐使用 Unicode 编码

      常见的正则表达式的文档都是关于英文(ASCII字符)的,英文开发者通常也只需要处理ASCII字符,不需要处理中文这类多字符的字符.不过,依照李处ASCII字符的方式处理中文字符,就有可能出错.   ...

  5. 嵌套的frame

    自动化的测试中,iframe的嵌套也是很常见的,对于嵌套的iframe,我们处理的方式是先进入到iframe的父节点, 再进入到子节点,然后可以对子节点里面的对象进行处理和操作.如下的html代码效果 ...

  6. 浅谈 JVM 结构体系、类加载、JDK JRE JVM 三者的关系

    一.java类,创建.编译.到运行的工程: 1.随便建一个Java类,保存后就是一个.java文件, 2.然后我们使用 javac命令编译 .java文件,生产 .class文件. 3.再然后使用 j ...

  7. Python中的sorted函数

    今天在做一个中文文本分类的项目,遇到了一个sorted函数,发现并不会用... 记录一下: sorted(list, key, reverse) list是给定的列表: key是排序过程调用的函数,也 ...

  8. java正则匹配正则表达式

    1.简单匹配小案例 public static void main( String[] args ){ // 按指定模式在字符串查找 String line = "This order wa ...

  9. Go语言入门篇-命令 与 语法

    一.命令基础 1. go run : 用于运行命令源码文件(如:go run helloworld.go) 只能接受一个命令源码文件以及若干个库源码文件作为文件参数 其内部操作步骤: (1)先编译源码 ...

  10. zookeeper 分布式协调服务

    分布式协调服务作用是将多机协调的职责从分布式应用中独立出来,以减少系统的耦合性和增加扩展性. 而zookeeper采用分布式中经典的主从架构:master->slave,通常以动态的存储分布式应 ...