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 ...
随机推荐
- FastDFS在centos上的安装配置与使用
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务.(百 ...
- Oracle之自定义函数
数据库中函数包含四个部分:声明.返回值.函数体和异常处理. --没有参数的函数 create or replace function get_user return varchar2 is v_use ...
- python 学习笔记3(循环方式;list初始化;循环对象/生成器/表推导;函数对象;异常处理)
### Python的强大很大一部分原因在于,它提供有很多已经写好的,可以现成用的对象 16. 循环方式笔记: 1)range(0, 8, 2) #(上限,下限,步长) 可以实现对元素或者下标的 ...
- TCP和Http的区别
相信不少初学手机联网开发的朋友都想知道Http与Socket连接究竟有什么区别,希望通过自己的浅显理解能对初学者有所帮助. 1.TCP连接 手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可 ...
- linux 的jdk安装
1.1 解压上传的安装包 1.2 创建安装目录 1.3 将解压后的目录移动到安装目录 1.4 配置环境变量 修改www.qixoo.qixoo.com/etc/profile文件 ...
- php编程常用经验
1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...
- classpath、path、JAVA_HOME的作用
CLASSPATH是什么?它的作用是什么? 它是javac编译器的一个环境变量. 它的作用与import.package关键字有关. 当你写下improt java.util.*时,编译器面对impo ...
- redis.conf
redis示例配置文件 分类: redis2013-10-22 16:39 427人阅读 评论(0) 收藏 举报 转载自https://raw.github.com/antirez/redis/2.6 ...
- JavaScript常见调试方法
编辑导语:javascript调试方法,常见使用alert和console来定位出错和输出的结果是否是想要的,在chrome中,还可以使用断点来看运行的情况等,本文介绍了比较全面的调试方法,你知道co ...
- Why The Golden Age Of Machine Learning is Just Beginning
Why The Golden Age Of Machine Learning is Just Beginning Even though the buzz around neural networks ...