[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 ...
随机推荐
- C# 创建Windows服务。服务功能:定时操作数据库
一.创建window服务 1.新建项目-->选择Windows服务.默认生成文件包括Program.cs,Service1.cs 2.在Service1.cs添加如下代码: System.T ...
- UML-测试驱动开发
1.什么是测试驱动开发? 测试驱动开发(TDD)是迭代和敏捷XP方法提倡的优秀实践,该实践为测试测试优先开发.不仅包含单元测试. 2.为什么测试驱动开发? 1).最大提升代码稳定性 2).澄清接口和行 ...
- vue安装插件指定版本
安装插件指定版本 npm install 插件名称@2.9.6 --save 查看需要安装插件的版本记录 npm view 插件名称 versions --json
- 精准医疗|研发药物|Encode|roadmap|
生物医学大数据 精准医疗 研发药物:特异性靶点&过表达靶点 Encode &roadmap找组织特异性的表观遗传学标记.TF.DNA甲基化的动态变化等信息. 生物大数据的标准化与整合- ...
- Try setting a different JdbcType for this parameter or a different configuration property. Cause: org.postgresql.util.PSQLException: 栏位索引超过许可范围:2,栏位数:1
执行mybaits sql <delete id="delete4BatchesByLineCi" parameterType="java.util.List&qu ...
- 关于 tf.image.crop_and_resize的使用
https://blog.csdn.net/m0_38024332/article/details/81779544 关于 tf.image.crop_and_resize 的使用 最近在学习fas ...
- vue拖拽插件(弹框拖拽)
// =======拖拽 插件 cnpm install vuedraggableimport draggable from 'vuedraggable' <draggable v-model= ...
- 乳草的侵占(BFS)
armer John一直努力让他的草地充满鲜美多汁的而又健康的牧草.可惜天不从人愿,他在植物大战人类中败下阵来.邪恶的乳草已经在他的农场的西北部份占领了一片立足之地. 草地像往常一样,被分割成一个高度 ...
- ubuntu14.10安装gitlab
1 换源: # curl https://packages.gitlab.com/gpg.key 2> /dev/null | sudo apt-key add - &>/dev/ ...
- BSC软件交流-BS
管理体系的提升公司.部门 关键指标 体系EXCEL记录的方式 较老,不够系统化BSC模式 测评.咨询.绩效软件目标地图 ,任务 目标 分解 平台?手机端? 集成 钉钉? paas平台?基础数据的获取团 ...