[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 ...
随机推荐
- java android 捕获未处理异常
1. 定义一个异常处理类 public class ExceptionHandler implements Thread.UncaughtExceptionHandler { public Excep ...
- “数据上帝” Jeff Hammerbacher
出生于1983年的数学天才Jeff Hammerbacher在23岁时加入了Facebook,一手组建起数据分析队伍.他是“数据科学”(data science)一词的提出者之一,被人们称为“数据上帝 ...
- 黄聪:通过 itms:services://? 在线安装ipa ,跨过appstore
1.需要一个html文件,引导下载用户在线安装ipa <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN&quo ...
- Flask--templates-多个模板文件,视图函数如何判断查询路径
结论:以当前视图的模板为基准,查找模板文件,如果没有找到就会报错,如果需要更换模板的访问路径,可以修改__name__参数. 参考资料: https://blog.csdn.net/f70408410 ...
- centos7-软件安装-redis3.2
wget方式下载redis3.2 wget http://download.redis.io/releases/redis-stable.tar.gz 命令行下载redis,此命令会保存redis至当 ...
- day2模块初识别
sys_mod.py import sys print(sys.argv) #['C:/Users/Administrator/desktop/s17/day2/sys_mod.py'] 打印脚本的绝 ...
- time&datetime
关于time模块的代码部分 1 #_*_coding:utf-8_*_ 2 __author__ = 'Alex Li' 3 4 import time 5 6 7 # print(time.cloc ...
- git之sourceTree操作流程
1x.sourceTree的使用流程 12.Git管理工具对比(GitBash.EGit.SourceTree) 11.SourceTree使用SSH克隆码云项目 ====== 1x.source ...
- lecune入门示例
注意:本示例中的lucene版本需在jdk7以上使用. 一.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- tips:Jquery的attr和prop的区别
Jquery的attr和prop的区别 描述:想做一个复选框checkbox全选的功能,当勾选全选后,将子项的复选框状态设置成一致的, 但遇到了一个问题,就是attr函数并不能改变子项的checkbo ...