Java for LeetCode 078 Subsets
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],
[]
]
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的更多相关文章
- Java for LeetCode 090 Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets. Not ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 在VS2012中编译WinXP兼容的程序
VS2012默认是不兼容Windows XP的,编译链接出来的程序只能在Windows Vista及以上版本的操作系统上运行.可是有时需要在Windows XP上运行,又不得不用VS2012(例如用了 ...
- BZOJ1432 [ZJOI2009]Function
Description Input 一行两个整数n; k. Output 一行一个整数,表示n 个函数第k 层最少能由多少段组成. Sample Input 1 1 Sample Output 1 H ...
- HackerRank Ice Cream Parlor
传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...
- jquery插件实现上下滑动翻页效果
<!DOCTYPE > <meta charset="utf-8" /> <head> <title>测试jquery</ti ...
- ASP+ACCESS手工注入详解
SQL注入这么长时间,看见有的朋友还是不会手工注入,那么我来演示一下.高手略过. 我们大家知道,一般注入产生在没经过虑的变量上,像ID?=XX这样的. 下面以这个网址为例: http://zsb.xx ...
- 面向对象分析设计-------02UML+UML各种图形及作用
一.UML是什么?UML有什么用? 二.UML的历史 三.UML的上层结构(Superstructure) 四.UML建模工具 五.UML的图(重点) 1.用例图(use case diagram) ...
- 编译安装apache-2.4.18
apache安装时, 必须要apr和apr-util, 这两个包是必须的 当下载apache的版本过高, 如: apache-2.4.18, 那么要求的apr或apu=apr-util版本将至少在1. ...
- 将DataTable导出为Excel C#
/// <summary> /// 导出Excel /// </summary> /// <param name="dt">DataTable& ...
- 将List<int> 转换为用逗号连接为字符串
List<, , , , }; string str = String.Join(",", testList.ConvertAll<string>(new Con ...
- chrome 调试基本信息学习
学习链接: remote-debugging-port相关: http://blog.chromium.org/2011/05/remote-debugging-with-chrome-develop ...