[leetcode]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],
[]
]
题意:
给定一个含不同整数的集合,返回其所有的子集
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数组子集的更多相关文章
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- LeetCode 78 Subsets (所有子集)
题目链接:https://leetcode.com/problems/subsets/#/description 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
- leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- [LeetCode] 90. Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- 78 Subsets(求子集Medium)
题目意思:求解一个数组的所有子集,子集内的元素增序排列eg:[1,3,2] result:[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]思路:这是一个递推的过程 [] ...
- Leetcode#78 Subsets
原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...
- LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets. Note: The solution set must not ...
- [LeetCode] 78. Subsets tag: backtracking
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
随机推荐
- selenium 网络请求
selenium 网络请求 browser.find_element_by_id("id的name")browser.find_element("")brows ...
- golang-generate-1pixel-image
package main import ( "bytes" "encoding/base64" "flag" "html/temp ...
- 在 sql 语句出现 warning 之后,立刻执行 `show warnings;` 就可以看到 warning 提示信息
在 sql 语句出现 warning 之后,立刻执行 show warnings; 就可以看到 warning 提示信息
- git之sourceTree使用github和码云的代码小结
16.使用git出现的错误记录 15. Permission denied (publickey)错误: git远程库与本地库同步 git设置ssh公钥 Bad escape character ' ...
- jvm--深入理解java虚拟机 精华总结(面试)(转)
深入理解java虚拟机 精华总结(面试)(转) 原文地址:http://www.cnblogs.com/prayers/p/5515245.html 一.运行时数据区域 3 1.1 程序计数器 3 1 ...
- ubuntu下的Nessus插件更新
00x1: 记录下nessus插件离线更新,免得每次度娘我Nessus是放在虚拟机里面. 00x2: nessus 插件更新地址: https://plugins.nessus.org/v2/offl ...
- Cookie的存活时间
1. 默认情况下,cookie数据保存到内存里,当浏览器关闭后,Cookie数据被销毁 2. 持久化存储: setMaxAge(int seconds) 1. 正数:将Cookie数据写到硬盘的文件中 ...
- normalization正规化
用到sklearn模块 from sklearn import preprocessing用preprocessing.scale正规化 print(preprocessing.scale(a))
- day31网络编程
网络编程1. 目标:编写一个C/S架构的软件 C/S: Client(用户端)--------基于网络----------Server(服务端) B/S: Browser-------基于 ...
- WebService . Schema约束
1. namespace 相当于schema文件的id 2. targetNamespace属性 用来指定schema文件的namespace的值 3. xmlns属性 引入一个约束, 它的值是一个s ...