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 ...
随机推荐
- Oracle11g数据文件DBF迁移
最近接手了一个以前同事遗留下来的项目,时机比较敏感,因为要召开11届全国少数名族运动会.建国70周年,以及香港暴乱,其中网站上挂载有十几个系统的入口链接,不巧的是其中一个系统存在若口令,被公安部安全局 ...
- Spoj375 Qtree--树链剖分
Spoj375 Qtree给一棵共有 n(n · 10000) 个结点的树, 每条边都有一个权值, 要求维护一个数据结构, 支持如下操作: 1. 修改某条边的权值; 2. 询问某两个结点之间的唯一通路 ...
- 操作系统安全 - 提权 - Windows提权 - 汇总
CVE_2019-1388 Date: -- 影响范围: SERVER ====== Windows 2008r2 ** link OPENED AS SYSTEM ** Windows 2012r2 ...
- note.js使用express创建项目的步骤以及ip和端口配置
1.安装express npm install -g express 2.创建项目 express -e 项目名称 3.打开cmd进入项目目录安装依赖 npm install 4.打开配置文件./bi ...
- Zend_Cache的使用
一.Zend_Cache快速浏览 Zend_Cache 提供了一个缓存任何数据的一般方法. 在Zend Framework中缓存由前端操作,同时通过后端适配器(File, Sqlite, Memcac ...
- HDU - 1845 Jimmy’s Assignment (二分匹配)
Description Jimmy is studying Advanced Graph Algorithms at his university. His most recent assignmen ...
- Text Autosizer&&解决移动端网页文本字体怪异增大问题
在做移动端页面时,有时你设置了字体大小,有的部分即使设置了行内样式也不生效,而有些显示正常,这个特性就是Text Autosizer在搞鬼. 以下是解决方案: ①给元素设置 -webkit-text- ...
- 利用CSS的translate属性或利用CSS实现图片居中的效果
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C# 静态方法 静态属性 调用静态方法
C#的类中可以包含两种方法:静态方法和非静态方法. 使用了static 修饰符的方法为静态方法,反之则是非静态方法. 静态方法是一种 特殊的成员方法,它不属于类的某一个具体的实例,而是属于类本身.所以 ...
- Chrome开发者工具详解(二)之使用断点调试代码下
JS调试技巧 技巧一:格式化压缩代码 技巧二:快速跳转到某个断点的位置 右侧的Breakpoints会汇总你在JS文件所有打过的断点,点击跟checkbox同一行的会暂时取消这个断点,若是点击chec ...