Given a set of distinct integers, nums, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • 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],
[]
]
解题思路:
和上题十分相似,修改上题代码即可,JAVA实现如下:
    static public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
list.add(new ArrayList<Integer>());
Arrays.sort(nums);
for(int i=1;i<=nums.length;i++)
dfs(list, nums.length, i, 0,nums,-1);
return list;
} static List<Integer> alist = new ArrayList<Integer>(); static void dfs(List<List<Integer>> list, int n, int k, int depth,int[] nums,int last) {
if (depth >= k) {
list.add(new ArrayList<Integer>(alist));
return;
}
for (int i = last+1; i <= n-k+depth; i++) {
alist.add(nums[i]);
dfs(list, n, k, depth + 1,nums,i);
alist.remove(alist.size() - 1);
}
}

Java for LeetCode 078 Subsets的更多相关文章

  1. Java for LeetCode 090 Subsets II

    Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...

  2. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  3. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  4. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  6. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

  7. Java for LeetCode 200 Number of Islands

    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

  8. Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. Java for LeetCode 154 Find Minimum in Rotated Sorted Array II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. Excel 计算 tips

    1.  对一列数据想看看,distinct的结果 选中数据区域(包含列名),插入pivot table 2. 想检查一个单元格的值在不在某一列中,并返回标志值 =IF (COUNTIF(B:B,A1) ...

  2. 内部类访问局部变量的时候,为什么变量必须加上final修饰

    这里的局部变量就是在类方法中的变量,能访问方法中变量的类当然也是局部内部类了.我们都知道,局部变量在所处的函数执行完之后就释放了,但是内部类对象如果还有引用指向的话它是还存在的.例如下面的代码: cl ...

  3. jquery ajax提交表单数据的两种方式

    http://www.kwstu.com/ArticleView/kwstu_201331316441313 貌似AJAX越来越火了,作为一个WEB程序开发者要是不会这个感觉就要落伍,甚至有可能在求职 ...

  4. 用多态来实现U盘,Mp3,移动硬盘和电脑的对接,读取写入数据。

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. js中改变不同的div属性值的操作

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. ECSHOP去版权标志删除Powered by ECShop(转)

    ECSHOP去版权标志删除Powered by ECShop ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-11   各位ECSHOP网店系统用户大家好 ...

  7. html5+监听设备加速度变化信息

    watchAcceleration 监听设备加速度变化信息 Number plus.accelerometer.watchAcceleration( successCB, errorCB, optio ...

  8. HTTP负载测试——Tsung

    参考资料:http://blog.jobbole.com/87509/ 如何生成每秒百万级别的 HTTP 请求? 在进行负责测试时要牢记一件重要的事:你能在 Linux 上建立多少个 socket 连 ...

  9. php 单态设计模式

    单态设计模式通常包含以下三点: · 一个私有的 构造方法:(确保用户无法通过创建对象对其进行实例化) · 一个公有的 静态的 方法:(负责对其本身进行实例化) · 一个私有的 静态的 属性:(用于保存 ...

  10. .NET中使用Memcached的相关资源整理

    Memcached官方站点:http://www.danga.com/memcached/ Memcached Win32 1.2.6下载:http://code.jellycan.com/memca ...