Java for LeetCode 077 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
public static void main(String args[]) {
List<List<Integer>> list = combine(5, 2);
System.out.println(list.size());
for (List<Integer> alist : list)
System.out.println(alist + "*");
} static public List<List<Integer>> combine(int n, int k) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (n <= 0 || k > n)
return list;
dfs(list, n, k, 0);
return list;
} static List<Integer> alist = new ArrayList<Integer>(); static void dfs(List<List<Integer>> list, int n, int k, int depth) {
if (depth >= k) {
list.add(new ArrayList<Integer>(alist));
return;
}
if (depth == 0)
for (int i = 1; i <= n - k + 1; i++) {
alist.add(i);
dfs(list, n, k, depth + 1);
alist.remove(alist.size() - 1);
}
else
for (int i = 1; i <= n - alist.get(alist.size() - 1) + k - depth - 1; i++) {
alist.add(alist.get(alist.size() - 1) + i);
dfs(list, n, k, depth + 1);
alist.remove(alist.size() - 1);
}
}
Java for LeetCode 077 Combinations的更多相关文章
- 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 ...
随机推荐
- 【POJ 1416】Shredding Company
题 题意 给你一个target number,和一个最多六位的数num,让你把数分段,使总和最接近但不大于target number. 如果只有一种方法就输出总和.分段,如果有多种方法,输出rejec ...
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
- groovy-位运算
从Groovy 1.0 beta 10开始,Groovy支持位运算:<<. >>, >>>, |, &, ^, and ~. 下表列出了位运算的操作符 ...
- hihoCoder 1195 高斯消元.一
传送门 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:喂不得了啦,那边便利店的薯片半价了! 小Hi:啥?! 小Ho:那边的便利店在打折促销啊. 小Hi:走走走, ...
- 一个完整的编译器前端-A.1 源语言
这个语言的一个程序由一个块组成,该块中包含可选的声明和语句.语法符号basic表示基本类型. program –> block block –> { decls stmts } dec ...
- linux虚拟机系统的复制或克隆后续问题解决!
前言 加快创建hadoop或spark集群,方法有两种途径:克隆或复制.其实啊,我最近,再返回写下本博文,理清下思路. 比如,你在你的一台电脑里,安装虚拟机.已经搭建好了hadoop或spark集群. ...
- 使用easyUI 创建复杂的toolbar到datagrid
http://www.cnblogs.com/javaexam2/archive/2012/08/10/2632649.html @author YHC datagrid 的toolbar能包含的不仅 ...
- 代码注册广播接收者&利用广播调用服务的方法服务声命周期(混合开启)
1)说明文档: 2)效果演示: 3)代码演示:
- 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...
- Socket 入门- 客户端回射程序
结果输出:------------------------------------------------------客户端:xx@xxxxxx:~/Public/C$ ./postBackCli.o ...