[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 ...
随机推荐
- UVA 10534 LCS变种题
求一个序列中 的2*n-1个数字 ,前n+1个数字为严格升序 后n+1个为严格降序,求最长的长度 一开始还没想清楚怎么解,其实就是一个LCS问题,从头到尾以及反序求一下LCS 由于 d[i]为包含了自 ...
- 不重复,distinct,row_number() over(partition by)
1.查询不重复的字段 select distinct name from table 2.查询某个字段不重复的,所有内容 sql根据某一个字段重复只取第一条数据 select s.* from ( s ...
- 每天一杯C_C89、C99、C11等之C语言标准
C语言的伟大之处在于C语言还是一个国际标准,这只“无形的手”掌控者其他派生语言和计算机的各个方面.起关于C语言被发明之后,ANSI和ISO相继发布关于C语言的标准.关于C90和C99,C89和C99容 ...
- C++构造函数概念作用
作用: 对对象进行初始化,如给成员变量赋初值,而不用专门再写初始化函数. 防止有些对象没被初始化就使用,导致程序出错. 要求: 名字与类名相同,可以有参数,但不能有返回值(void也不行) 编译时: ...
- python+Sqlite+Dataframe打造金融股票数据结构
5. 本地数据库 很简单的用本地Sqlite查找股票数据. DataSource类,返回的是Dataframe物件.这个Dataframe物件,在之后的业务,如计算股票指标,还需要特别处理. impo ...
- textarea高度自适应解决方法
引入autosize.js <script src="./autosize.js"></script> autosize(document.getEleme ...
- DNS和hosts
https://zhidao.baidu.com/question/571487394.html 还有ip地址和域名 域名是唯一的 ip也是唯一的 但是一个域名可以对应多个ip(就好比百度只有一个域名 ...
- Linux系统的限制
1.总结系统限制有: /proc/sys/kernel/pid_max #查系统支持的最大线程数,一般会很大,相当于理论值 /proc/sys/kernel/thread-max m ...
- 洛谷 P1258 小车问题
题目传送门 解题思路: 首先,每个人都要做一次车,而且两个人要同时到达,这样才能使总时间最短. 那么,我们设起点为A,终点为B,小车先带甲开到C点后甲下车走到B点,同时小车掉头与已经走到D点的乙相向而 ...
- 理论优美的深度信念网络--Hinton北大最新演讲
什么是深度信念网络 深度信念网络是第一批成功应用深度架构训练的非卷积模型之一. 在引入深度信念网络之前,研究社区通常认为深度模型太难优化,还不如使用易于优化的浅层ML模型.2006年,Hinton等研 ...