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的更多相关文章

  1. 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 ...

  2. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  3. 刷题78. Subsets

    一.题目说明 题目78. Subsets,给一列整数,求所有可能的子集.题目难度是Medium! 二.我的解答 这个题目,前面做过一个类似的,相当于求闭包: 刷题22. Generate Parent ...

  4. 78. Subsets 求所有子集(有重复就continue)

    [抄题]: Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...

  5. [LeetCode] 78. Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  6. Leetcode#78 Subsets

    原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...

  7. 78. Subsets

    题目: Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset mus ...

  8. 78 Subsets(求子集Medium)

    题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...

  9. LeetCode OJ 78. Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

随机推荐

  1. centos通过yum安装php

    1.添加php的yum软件仓库 sudo rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm 2.安装php相关软件,执行过程中全部选择ye ...

  2. html标签title属性效果优化

    html标签title属性效果不友好,最致命的是响应慢,体验不好,JQuery-UI提供了很好的支持.在jquery.tip的基础上又做了修改,这样子定制效果更强. 代码如下: <!DOCTYP ...

  3. 《Java核心技术卷I》观赏指南

    Tomxin7 如果你有想看书的计划,但是还在纠结哪些书值得看,可以简单看看"观赏指南"系列,本文会简单列出书中内容,给还没有买书的朋友提供一个参考. 前言 秋招过去很久了,虽然在 ...

  4. Java并发分析—Lock

    1.Lock 和 Condition 当使用synchronied进行同步时,可以在同步代码块中只用常用的wait和notify等方法,在使用显示锁的时候,将通过Condition对象与任意Lock实 ...

  5. JDBC<android studio,kotlin>

    工具:mysql 5.6.19,mysql-connector-java-5.1.48.jar,android stuido&android studio自带模拟器 1.在mysql数据库中新 ...

  6. Linux CMD 笔记 & 正则表达式

    一.linux bash 1. 进程名查找kill ps -ef | grep xxxx| grep -v grep| cut -c 9-15 | xargs kill -9 2.端口号kill 占用 ...

  7. @Autowired的几个使用细节

    1.使用@Autowired的当前类也必须由spring容器托管(打@Coponent.@Controller.@Service .@repository) 2.不管是public 和  privat ...

  8. java类的实例化顺序

    1. 父类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 2. 子类静态成员和静态初始化块 ,按在代码中出现的顺序依次执行 3. 父类实例成员和实例初始化块 ,按在代码中出现的顺序依次执行 4 ...

  9. Cutting Sticks UVA - 10003(DP 仍有不明白的地方)

    题意:对一根长为l的木棒进行切割,给出n个切割点,每次切割的价值,等于需要切割的木头长度. 一开始理解错了,认为切割点时根据当前木条的左端点往右推算. 实际上,左端点始终是不变的一直是0,右端点一直是 ...

  10. 用IDLE调试python程序

    1. 设置断点 先放例子: import pdb a=1 b=10 pdb.set_trace()#这里是断点 c=a+b print(c) import pdb 后,用pdb.set_trace() ...