[LC] 78. Subsets
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],
[]
] Time: O(2^n)
Space: O(N)
class Solution:
def subsets(self, nums: 'List[int]') -> 'List[List[int]]':
res = []
if nums is None or len(nums) == 0:
return res
self.helper(nums, 0, [], res)
return res def helper(self, nums, index, combination, combinations):
combinations.append(list(combination))
for i in range(index, len(nums)):
combination.append(nums[i])
self.helper(nums, i + 1, combination, combinations)
combination.pop()
请问为什么需要写成list(combination), combination本身不就是list吗? 为什么把list()去掉append的就都是[]了?
这里牵扯到一个copy问题,如果不加list,那么copy的就是combination的reference,因此list之后的改变都会导致之前加入值的改变,加上list()之后就是建立了一个当前combination的copy,之后无论list如何改变,就不变了
因为append进去的不是一个数,而是一个object(这里就是list)。之后这个object被改变了的话,之前append进去的那个list也会跟着变。比如append [1] 之后,把这个[1] 改成[2] 再append进去,得到的会是[ [2], [2] ] 而不是[ [1], [2] ]
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) {
return res;
}
helper(res, new ArrayList<>(), nums, 0);
return res;
}
private void helper(List<List<Integer>> res, List<Integer> list, int[] nums, int start) {
res.add(new ArrayList<>(list));
// use start to ignore the previous numbers
for (int i = start; i < nums.length; i++) {
list.add(nums[i]);
// use i not index b/c index smaller than i
helper(res, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}
public class Solution {
public List<String> subSets(String set) {
// Write your solution here.
List<String> res = new ArrayList<>();
if (set == null) {
return res;
}
StringBuilder sb = new StringBuilder();
helper(res, 0, set, sb);
return res;
}
private void helper(List<String> res, int index, String s, StringBuilder sb) {
if (index == s.length()) {
res.add(sb.toString());
return;
}
helper(res, index + 1, s, sb);
sb.append(s.charAt(index));
helper(res, index + 1, s, sb);
sb.deleteCharAt(sb.length() - 1);
}
}
[LC] 78. Subsets的更多相关文章
- 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning
78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- 刷题78. Subsets
一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...
- 78. Subsets 求所有子集(有重复就continue)
[抄题]: Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- Leetcode#78 Subsets
原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...
- 78. Subsets
题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- LeetCode OJ 78. Subsets
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
随机推荐
- 201803-1 跳一跳 Java
思路: 一个变量plus记录叠加的数,遇到2就+2 import java.util.Scanner; public class Main { public static void main(Stri ...
- jupyter notebook 安装配置使用,+目录插件安装
1.安装 pip3 install jupyter 2.配置 2.1. 生成一个 notebook 配置文件 jupyter notebook --generate-config /root/.jup ...
- 0CTF-2016-piapiapia-PHP反序列化长度变化尾部字符串逃逸
0X00 扫描一下网站目录,得到网站源码,这里说下工具使用的是dirmap,亲测御剑不好用... 0x01 审计源码: index.php <?php require_once('class.p ...
- Cookie的作用范围、设置、创建、获取的方法
cookie的作用范围 同一浏览器,同一路径 默认情况下, 上级目录设置的cookie,下级目录可以获取到, 而下级目录设置的cookie,上级目录不能获取. 即:在一个页面设置cookie,那么这个 ...
- Tomcat server.xml常用配置 含有外带文件及默认host
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE server-xml [<!ENTITY ...
- mysql 5.6 cmake的安装
# cmake \-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \-DMYSQL_DATADIR=/usr/local/mysql/data \-DSYSCONFDI ...
- Python笔记_第四篇_高阶编程_检测_1.对函数进行单元检测
1. 对函数进行单元检测: 单元检测: 作用:用来对一个函数.一个类.一个模块进行正确性校验工作. 结果: * 单元测试通过,说明我们测试函数的功能正确. * 单元测试不通过,说明函数有BUG,要么测 ...
- CI项目设计Redis队列
项目开发过程中需要设计提供可平衡的处理多个用户请求的队列. 需求: 当用户登录后,查看系统中已经登录的管理员队列,然后查看后台管理员的处理能力,如果已经不能处理新的请求,则把该管理员从处理队列中删除, ...
- Java基础篇 - 强引用、弱引用、软引用和虚引用
Java基础篇 - 强引用.弱引用.软引用和虚引用 原创零壹技术栈 最后发布于2018-09-09 08:58:21 阅读数 4936 收藏展开前言Java执行GC判断对象是否存活有两种方式其中一种是 ...
- 计量经济与时间序列_ACF与PACF标准差(均标准误)的计算(含代码)
1 我们对于acf和pacf值计算完毕之后,在需要计算两个数值的标准差. 2 acf和pacf的标准差计算略有不同.acf的标准差是一个移动过程,而pacf是一个相对固定过程. 3 我们继 ...