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],
[]
]

题意:

给定一个含不同整数的集合,返回其所有的子集

assumption:

1. do we need to return if subsets is empty?

2. what kind of order to return if there are many subsets

3. dupulicates in the given array?

solution:

1.

example:

nums = [1,   2,   3]

^

index

level0          [ ]

/  |   \

level1     [1]  [2]  [3]

/    \

level2 [1,2]   [1,3]

/

level3  [1,2,3]

level0:  add [] to result[ [] ],   use pointer: index to scan given array, add current element to path [1], pass [1] to next level,

level1:  add[1] to result[[][1]],  treat index element as a start, pick one in the remaining and added to the path [1,2], pass[1,2] to next level

level2: add[1,2] to result[[][1][1,2]] treat index element as a start, pick one in the remaining and added to the path [1,2, 3], pass[1,2,3] to next level

level3: add[1,2,3] to result[[][1][1,2][1,2,3]]

二、High Level带着面试官walk through:

生成ArrayList作为每条path的记录,扔到result里

以当前index为开头,call helper function, 使得在index之后剩下可用的item中选一个加到当前path后面

代码:

 class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
helper(nums,0, path, result);
return result;
}
private void helper(int[]nums, int index, List<Integer> path, List<List<Integer>> result){
result.add(new ArrayList<>(path)); for(int i = index; i< nums.length; i++){
path.add(nums[i]);
helper(nums, i+1, path, result);
path.remove(path.size()-1);
}
}
}

[leetcode]78. Subsets数组子集的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

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

  2. LeetCode 78 Subsets (所有子集)

    题目链接:https://leetcode.com/problems/subsets/#/description   给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...

  3. leetCode 78.Subsets (子集) 解题思路和方法

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

  4. [LeetCode] 78. Subsets 子集合

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

  5. [LeetCode] 90. Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. 78 Subsets(求子集Medium)

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

  7. Leetcode#78 Subsets

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

  8. LeetCode 78. Subsets(子集合)

    Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...

  9. [LeetCode] 78. Subsets tag: backtracking

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

随机推荐

  1. GoStudy——解决vscode中golang插件依赖安装失败问题

    vscode中安装ms-vscode.go插件后可以开启对go语言的支持,ms-vscode.go插件需要依赖一些工具,安装完成后提示 Installing github.com/nsf/gocode ...

  2. XShell停止滚屏,禁止滚动

     Ctrl+S:锁定当前屏幕  Ctrl+Q:解锁当前屏幕 Ctrl+Alt+]  进入命令输入状态

  3. HTTP Request & Response

    Request & Response header details can be found here The request method indicates the method to b ...

  4. Python装饰器的调用过程

    在Python学习的过程中,装饰器是比较难理解的一个应用.本人也在学习期间也遇到很多坑,现将装饰器的基本调用过程总结一下. 首先,装饰器用到了“闭包”,而“闭包”是学习装饰器的基础,所以在讲装饰器之前 ...

  5. Gitlab CI 持续集成的完整实践

    Gitlab CI 持续集成的完整实践 本着公司团队初创,又在空档期想搞点事情,搭建了私有Gitlab的契机,顺便把持续集成搭建起,实现了对Python服务端代码的单元测试.静态代码分析和接口测试的持 ...

  6. 从OsChina Git下载项目到MyEclipse中

    前提是,拥有权限下载 1.进入MyEclipse,点击File-->Import,选择Git,点击“Next”,如下图: , 2.选择“URI”,点击"Next" 3.输入项 ...

  7. 第26课 可变参数模板(7)_any和variant类的实现

    1. any类的实现 (1)any类: ①是一个特殊的,只能容纳一个元素的容器,它可以擦除类型,可以将何任类型的值赋值给它. ②使用时,需要根据实际类型将any对象转换为实际的对象. (2)实现any ...

  8. js数据类型检测

    目录 1. typeof {} 2. {} instanceof Object 3. {}.constructor === Object 4. Object.property.toString.cal ...

  9. scrapy-items

    items定义字段名字 import scrapy class HrItem(scrapy.Item): # define the fields for your item here like: ti ...

  10. VS2015密匙--VS2015打开丢失msvcp140.dll--cannot find one or more components ,please reinstall the application

    win7旗舰版 64位 + vs2015 专业版 1.安装VS2015过程中可能需要用到的VS2015专业版钥匙:(测试,可用) HMGNV-WCYXV-X7G9W-YCX63-B98R2 2.VS2 ...