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. 【kAri OJ605】陈队的树

    时间限制 1000 ms 内存限制 65536 KB 题目描述 陈队有N棵树,有一天他突然想修剪一下这N棵树,他有M个修剪器,对于每个修剪器给出一个高度h,表示这个修剪器可以把某一棵高度超过h的树修剪 ...

  2. Fence 设备

    RHCS中必须有Fence设备,在设备为知故障发生时,Fence负责让占有浮动资源的设备与集群断开. REDHAT的fence device有两种, 内部fence设备: IBM RSAII卡,HP的 ...

  3. 【BZOJ】【1009】 【HNOI2008】GT考试

    DP/KMP/矩阵乘法 好神的题啊……跪了跪了 $n\leq 10^9$是什么鬼……我们还是先不要考虑这个鬼畜的玩意了>_> 用类似数位DP的思路,我们可以想到一个DP方程:$f[i][j ...

  4. BZOJ-1834 网络扩容 最小费用最大流+最大流+乱搞

    1834: [ZJOI2010]network 网络扩容 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 2269 Solved: 1136 [Submit ...

  5. springMVC实现防止重复提交

    参考文档:http://my.oschina.net/mushui/blog/143397

  6. BZOJ2460 [BeiJing2011]元素

    Description 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力越 ...

  7. Exceptionless 本地部署

    免费开源分布式系统日志收集框架 Exceptionless 前两天看到了这篇文章,亲身体会了下,确实不错,按照官方的文档试了试本地部署,折腾一番后终于成功,记下心得在此,不敢独享. 本地部署官方wik ...

  8. Bootstrap Modals(模态框)

    http://www.runoob.com/bootstrap/bootstrap-v2-modal-plugin.html 描述 Bootstrap Modals(模态框)是使用定制的 Jquery ...

  9. 分享一段Java搞笑的代码注释

    今天在群里看到有人分享了一段搞笑的注释代码,觉得挺好玩的,在这里收藏一下 // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // ...

  10. 关于updateElement接口

    1.bool UpdateGameElement(const struct_game_element& ele, gs_dbs_user_info_op_req& db_req, :: ...