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的更多相关文章

  1. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. [LeetCode] Subsets 子集合

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

  5. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  6. leetcode — subsets

    import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...

  7. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  8. [leetcode]Subsets @ Python

    原题地址:https://oj.leetcode.com/problems/subsets/ 题意:枚举所有子集. 解题思路:碰到这种问题,一律dfs. 代码: class Solution: # @ ...

  9. [Leetcode] Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  10. [LeetCode] Subsets (bfs的vector实现)

    Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...

随机推荐

  1. Struts2中动态方法的调用

    Struts2中动态方法调用就是为了解决一个action对应多个请求的处理,以免action太多. 主要有一下三种方法:指定method属性.感叹号方式和通配符方式.推荐使用第三种方式. 1.指定me ...

  2. CDS

    very nice artical talk about mergechangelog and cleardataset Delta and Data http://www.cnblogs.com/y ...

  3. x.1

    最近公司人事变动略频 昨日老板召集众骨干动员,谈心,表示有信心,没资金压力. 今日各种谈心,唉…… 人事姐姐约逻辑组长聊,美术主管就找上了我,一通倾述.内容实事求是,但是行业内各公司都这样,唉,还想着 ...

  4. impress.js webslide 参数

    data-transition-duration="2000" 改变切换场景的速度,默认1000data-perspective="500" 改变透视的深度,默 ...

  5. Yosemite系统怎么录制 iOS8设备屏幕

    我一年前一直想要的一个功能,发布时很想用.一直没找到 ,很巧的是今天被测试发现了. 感谢CCTV.自己在这里也记录下: 你好!    在 OS X Yosemite  系统中,QuickTime 支持 ...

  6. 字符串strcpy

    strcpy函数的表达方式: //把一个char组成的字符串循环右移n个,如:“abcdefghi",n=2,移动后"hiabcdefgh" #include <i ...

  7. 微软How old do I Look——初体验

    前段时间微软发布了一个可爱的网站how old.net,着实火了一把,全民体验魔镜魅力. 上传自己的靓照到http://www.how-old.net/,它就可以告诉你性别和年龄,大家还习惯称之为“颜 ...

  8. 学习SVG系列(5):SVG渐变

    SVG渐变 渐变是一种从一种颜色到另一种颜色的平滑过渡,可以把多个颜色的过渡应用到同一个元素. 渐变有两种: Linear Redial 线性渐变-<linearGradient> lin ...

  9. zynq中uboot的qspi启动报错及解决办法

    问题描述: 用u-boot-xlnx-v2016.3版本编译的uboot通过qspi flash启动出现如下错误: 尝试在uboot命令行输入"sf probe 0 0 0"挂载q ...

  10. Container With Most Water -- LeetCode 11

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai).  ...