LeetCode--078--子集(python)
给定一组不含重复元素的整数数组 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)的更多相关文章
- LeetCode:子集 II【90】
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
- 每日一题-——LeetCode(78)子集
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集).输入: nums = [1,2,3]输出:[ [3], [1], [2], [1,2,3], [1,3], [2, ...
- [LeetCode]题解(python):078 Subsets
题目来源 https://leetcode.com/problems/subsets/ Given a set of distinct integers, nums, return all possi ...
- [LeetCode]题解(python):090 Subsets II
题目来源 https://leetcode.com/problems/subsets-ii/ Given a collection of integers that might contain dup ...
- [LeetCode]题解(python):125 Valid Palindrome
题目来源 https://leetcode.com/problems/valid-palindrome/ Given a string, determine if it is a palindrome ...
- [LeetCode]题解(python):120 Triangle
题目来源 https://leetcode.com/problems/triangle/ Given a triangle, find the minimum path sum from top to ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- [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 ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
随机推荐
- 编译mysql时CMake Error at cmake/readline.cmake:85 (MESSAGE)
CMake Error at cmake/readline.cmake:85 (MESSAGE): Curses library not found. Please install appropr ...
- NIO组件之channel
Java NIO指的是new IO ,相对OIO,也称non-blocking IO,对应四种基本IO类型中的IO多路复用,主要有有三大核心组件,Channel(管道),Buffer(缓冲区),sel ...
- Spring MVC对日期处理的不友好问题
一.后台日期类型解析到前端 1.在springmvc的配置文件中添加这个.annotation-driven在配置文件中只配置一次 (此方法全局作用)<mvc:annotation-dr ...
- 【MM系列】SAP 交货单屏幕增强
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 交货单屏幕增强 前言部分 ...
- Cocos2d-X网络编程(2) Cocos2d中的网络通信协议——http协议
HTTP协议也叫超文本传输协议.是互联网广泛使用的通信协议,常用于B/S架构中. HTTP连接使用的是短连接形式,也就是"请求-响应"的方式,不仅在请求时需要先建立连接,而且需要客 ...
- Go语言入门篇-命令 与 语法
一.命令基础 1. go run : 用于运行命令源码文件(如:go run helloworld.go) 只能接受一个命令源码文件以及若干个库源码文件作为文件参数 其内部操作步骤: (1)先编译源码 ...
- webservice的一些理解
web services中如果用.NET的话,DataSet可以作为与客户端交互的一个返回值,因为DataSet实质上是XML.而SOAP WSDL等都是基于XML的. --------------- ...
- 红帽学习笔记[RHCSA] 第九课[文件归档、硬盘、分区以及自动挂载、Swap、链接]
文件归档 tar是什么 通过tar命令可以将大型文件汇集成一个文件(归档),注意没有压缩功能. 压缩方式 gzip 通过gzip过滤文档,使用最广泛 bzip2 通常比gzip压缩小,但是不如gzip ...
- Mybatis-学习笔记(3)mapper配置文件
1.mapper配置文件常用的元素 parameterMap已经废弃,老式风格的参数映射. 2.select元素 映射查询语句.#{...}用于预处理语句参数,通过JDBC,这样一个参数在SQL中会由 ...
- Larkin’s NOI
Larkin’s NOI Problem Description Larkin has been to Yantai to take part in NOI 2010!众所周知(do you know ...