[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 ...
随机推荐
- 安装win10 1703版本操作系统
1.使用UltraISO 全功能单文件 9.5.3.2900刻录工具,刻录iso文件到U盘里 2.默认刻录之后,U盘分区格式变成了fat32,而fat32单个文件无法超过4GB 而1703版本里面的i ...
- git bash的安装与配置
作业要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 1.下载安装配置用户名和邮箱. (1)下载安装Github配置 ...
- LDAP解决多个服务器多个samba,不能指定多个samba域 的问题
问题:在创建账号的时候,必须指定一个sambaDomain,但是只能指定一个,但是我有多个samba域要集成,那怎么办呢,怎么弄都只能登陆一个samba,不能所有的都登,经过反复的测试,反复的测试,找 ...
- Python递归解压缩多级.zip压缩包
参考如下代码(from:https://stackoverflow.com/questions/36285502/how-to-extract-zip-file-recursively-in-pyth ...
- Delphi操作Ini文件
Delphi提供了一个TInifile类,使我们可以非常灵活的处理INI文件 一.INI文件的结构[小节名]ini文件 关键字1=值1 关键子2=值2INI文件允许有多个小节, ...
- 洛谷P1636学画画
传送 这个题我们需要一个大胆的想法(虽然AC后看了题解知道这是个定理) (求证明qwq) 如果一个图有2或0个奇点,它就一定可以一笔画出,如果不是2或0个奇点,那答案就是奇点数/2 (私认为因为两个奇 ...
- finstrument-functions
2017-12-03 23:59:16 参考 如何快速地在每个函数入口处加入相同的语句? https://www.zhihu.com/question/56132218 做个存档 scj@scjCom ...
- NFS服务配置
FS服务会经常用于在网络上共享存储. 比如有3台机子A,B,C;他们都需要访问同一个目录,使用NFS, 只需要把图片都放在A上,然后A共享给B和C即可. 访问B和C时,是通过网络的方式访问A上的哪个目 ...
- 谷歌浏览器内核Cef js代码整理(一)
尊重作者原创,未经作者允许不得转载!作者:xtfnpgy,原文地址: https://www.cnblogs.com/xtfnpgy/p/9285359.html 一.js基础知识 <!-- ...
- 生产者-消费者(wait-notify实现)
使用wait/notify来实现生产者消费者时能够达到在线程阻塞的效果,这样就不会出现轮询,然后浪费cpu时间的目的.代码如下:1. 状态类,表示是否已经生产: package com.demo; p ...