LeetCode 78. Subsets(子集合)
Given a set of distinct integers, nums, return all possible subsets.
Note: The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
题目标签:Array
方法 1:
题目给了我们一个nums array,让我们找到所有的子集合。看到这种要找到所有的可能性的题目一般都要用到递归。我们设一个List List res,先把 [ ] 加入res。接着我们来根据原题中的例子来看一下。
[1, 2, 3]
我们依次把每一个数字的index,放到递归function 里去, 还要给一个 new ArrayList<>() 叫list 放到function 里。在递归function里,对于每一个index,首先把这个index 的数字加入list,然后把list 加入res 里。接着遍历它之后的所有数字,对于每一个数字,建立一个新的new ArrayList<>() 把这个list的值存入新的,继续递归下去。直到拿到的index 已经是最后一个数字了,把自己加入list,把list 加入res之后,直接跳过遍历,因为后面没有数字了,就返回了。我们来看一下例子
[1] [2] [3]
/ \ / \
[1,2] [1,3] [2,3]
/
[1,2,3]
顺序为[ [ ], [1], [1,2], [1,2,3], [1,3], [2], [2,3], [3] ]
Java Solution 1:
Runtime beats 23.24%
完成日期:07/24/2017
关键词:Array
关键点:递归
public class Solution
{
List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums)
{
List<Integer> empty = new ArrayList<>();
res.add(empty); if(nums == null || nums.length == 0)
return res; // sort nums array
Arrays.sort(nums); // pass each number into findSubset
for(int i=0; i<nums.length; i++)
findSubset(i, nums, new ArrayList<>()); return res;
} public void findSubset(int begin, int[] nums, List<Integer> list)
{
// add nums[begin] into list
list.add(nums[begin]); // add this list into res
res.add(list); // pass rest numbers to findSubset with list starting from [begin...
for(int i=begin+1; i<nums.length; i++)
{
List<Integer> temp = new ArrayList<>();
temp.addAll(list); findSubset(i, nums, temp);
} return;
}
}
参考资料:N/A
方法 2:
基本的原理和方法1一样,只不过在方法2中,可以利用list 的remove 把最后一个数字去除,然后继续与剩下的数字组成subset。这样的话就可以不需要在subsets function里利用for loop把每一个数字pass 给helper function了。方法 2 更加清晰简介,具体看code。
Java Solution 2:
Runtime beats 23.24%
完成日期:08/25/2017
关键词:Array
关键点:递归,当新的递归返回的时候把tmpRes list里的最后一个数字去除
public class Solution
{
public List<List<Integer>> subsets(int[] nums)
{
// sort nums array
Arrays.sort(nums);
// create res
List<List<Integer>> res = new ArrayList<>();
// call recursion function
helper(res, new ArrayList<>(), 0, nums); return res;
} public void helper(List<List<Integer>> res, List<Integer> tmpRes, int pos, int[] nums)
{
// here should be <= not just < because we need to add the new tmpRes in next recursion.
// Therefore, we need one more bound to add tmpRes
if(pos <= nums.length)
res.add(new ArrayList<>(tmpRes)); // once the new recursion is finished, remove the last number in the tmpRes and continue to
// add rest numbers to get new subsets
for(int i=pos; i<nums.length; i++)
{
tmpRes.add(nums[i]);
helper(res, tmpRes, i+1, nums);
tmpRes.remove(tmpRes.size()-1);
}
}
}
参考资料:
https://discuss.leetcode.com/topic/22638/very-simple-and-fast-java-solution/4
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
LeetCode 78. Subsets(子集合)的更多相关文章
- leetCode 78.Subsets (子集) 解题思路和方法
Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...
- [Leetcode 78]求子集 Subset
[题目] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The ...
- [LeetCode] 78. Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- Leetcode#78 Subsets
原题地址 有两种方法: 1. 对于序列S,其子集可以对应为一个二进制数,每一位对应集合中的某个数字,0代表不选,1代表选,比如S={1,2,3},则子集合就是3bit的所有二进制数. 所以,照着二进制 ...
- leetcode 78. Subsets 、90. Subsets II
第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- LeetCode 78 Subsets (所有子集)
题目链接:https://leetcode.com/problems/subsets/#/description 给出一个数组,数组中的元素各不相同,找到该集合的所有子集(包括空集和本身) 举例说 ...
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [LeetCode] 78. Subsets tag: backtracking
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
随机推荐
- 注解【介绍、基本Annotation、元Anntation、自定义注解、注入基本信息、对象】
什么是注解? 注解:Annotation-. 注解其实就是代码中的特殊标记,这些标记可以在编译.类加载.运行时被读取,并执行相对应的处理. 为什么我们需要用到注解? 传统的方式,我们是通过配置文件(x ...
- 双击打开Jar文件
最近发现个诡异的问题,java环境变量明明配好了.但是双击xx.jar文件,就是不能直接打开运行. 先想到了第一个解决办法: 运行cmd.exe,cd到jar目录,执行 javaw -jar xxx. ...
- Python NLP入门教程
本文简要介绍Python自然语言处理(NLP),使用Python的NLTK库.NLTK是Python的自然语言处理工具包,在NLP领域中,最常使用的一个Python库. 什么是NLP? 简单来说,自然 ...
- jmeter测试教程
http://www.cnblogs.com/TankXiao/p/4045439.html
- Rundeck部署和基本使用
rundeck 介绍 Rundeck 是一款能在数据中心或云环境中的日常业务中使程序自动化的开源软件.Rundeck 提供了大量功能,可以减轻耗时繁重的体力劳动.团队可以相互协作,分享如何过程自动化, ...
- C语言编程练习(一)
问题一: 问题描述:输入n个数,n<=100,找到其中最小的数和最大的数 输入样例: 4 1 2 3 4 输出样例:14 #include " ...
- Temperature hdu 3477
Temperature Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- PHP字符串函数-trim()实例用法
string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )此函数返回字符串 str 去除首尾空白字符后的 ...
- python web框架之Tornado
说Tornado之前分享几个前端不错的网站: -- Bootstrap http://www.bootcss.com/ -- Font Awesome http://fontawesome.io/ - ...
- zoj1494 暴力模拟 简单数学问题
Climbing Worm Time Limit: 2 Seconds Memory Limit:65536 KB An inch worm is at the bottom of a we ...