LeetCode-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],
[]
]
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
if(nums==null){
return null;
}
List<List<Integer>> resList=new ArrayList<List<Integer>>();
List<Integer> item=new ArrayList<Integer>();
Arrays.sort(nums);
backTracking(nums, 0, item, resList);
resList.add(new ArrayList<Integer>());
return resList;
}
public void backTracking(int[] nums, int start, List<Integer> item, List<List<Integer>> resList){
for(int i=start; i<nums.length; i++){
item.add(nums[i]);
resList.add(new ArrayList<Integer>(item));
backTracking(nums, i+1, item, resList);
item.remove(item.size()-1);
}
}
}
二刷:
1. 用 BFS 做
我们可以一位一位的网上叠加,比如对于题目中给的例子[1,2,3]来说,最开始是空集,那么我们现在要处理1,就在空集上加1,为[1],现在我们有两个自己[]和[1],下面我们来处理2,我们在之前的子集基础上,每个都加个2,可以分别得到[2],[1, 2],那么现在所有的子集合为[], [1], [2], [1, 2],同理处理3的情况可得[3], [1, 3], [2, 3], [1, 2, 3], 再加上之前的子集就是所有的子集合了,代码如下:
class Solution {
public List<List<Integer>> subsets(int[] nums) {
if(nums == null){
return null;
}
Queue<List<Integer>> queue = new LinkedList<>();
queue.offer(new ArrayList<Integer>());
for(int i = 0; i < nums.length; i++){
Queue<List<Integer>> temp = new LinkedList<>();
while(!queue.isEmpty()){
List<Integer> list = queue.poll();
List<Integer> newList = new ArrayList<>(list);
newList.add(nums[i]);
temp.add(list);
temp.add(newList);
}
queue = temp;
}
return new ArrayList<List<Integer>>(queue);
}
}
LeetCode-Subsets的更多相关文章
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets 子集合
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
- leetcode — subsets
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- [leetcode]Subsets @ Python
原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @ ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode] Subsets (bfs的vector实现)
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
随机推荐
- oracle问题
(1) 连通性: 注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小 监控中心负责统计各服务调用次数,调用时间等,统计先在内存汇 ...
- Tips for VNCServer config
Tips for VNCServer After the ClearCase server reboot by Jingwei, my vncserver background process is ...
- MyBatis 配置文件头部换行异常
INFO - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory ...
- SQL Server 2005 处理交叉表
假设有一个表如下: 日期 时间 售货金额 2006-01-02 早上 50 2006-01-02 中午 20 2006-01-02 晚上 30 2006-01-02 零晨 40 2006-01-03 ...
- [Java Basics3] XML, Unit testing
What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document ...
- html基础学习
<html> 与 </html> 之间的文本描述网页 <body> 与 </body> 之间的文本是可见的页面内容 <h1> 与 </ ...
- wamp出现You don’t have permission to access/on this server提示的解决方法
本地搭建wamp 输入http://127.0.0.1访问正常,当输入http://localhost/ apache出现You don't have permission to access/on ...
- HDU 5512
http://acm.hdu.edu.cn/showproblem.php?pid=5512 gcd(a,b)的位置都是可以选的,之后判断一下奇偶 #include <iostream> ...
- Xcode7 Cocoapods 安装或更新出现错误
好长时间没有玩过CocoaPods了,今天在执行 pod install --verbose --no-repo-update 的时候出现了错误如下 [MT] DVTAssertions: ASSER ...
- div 在页面上漂浮
<div id="codefans_net" style="position:absolute;z-index:5;"> <a& ...