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],
]
解题思路:
DFS,JAVA实现如下:
	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的更多相关文章

  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. Myeclipse报PermGen space异常的问题

    最好用的方法: 1)在myeclipse中windos——>preference——>Myeclipse——>servers——>Tomcat——>JDK(Optiona ...

  2. 畅所欲言第1期 - 从Viola&Jones的人脸检测说起

    转载自http://c.blog.sina.com.cn/profile.php?blogid=ab0aa22c890006v0 不少人认识我或者听说我的名字都是因为我过去做的关于人脸检测的工作,那么 ...

  3. appium跑demo简单实例讲解

    安装appium,设置 demo.pyfrom appium import webdriver #要装webdriver,方法查看http://www.cnblogs.com/sincoolvip/p ...

  4. C# WPF 显示图片和视频显示 EmuguCv、AForge.Net测试

    WPF 没有用到 PictureBox, 而是用Image代替. 下面我试着加载显示一个图片 . XAML <Image x:Name="srcImg"Width=" ...

  5. SpringMVC 2.5.6 noMapping

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  6. 使用UpdLock来扣减库存

    UPDLOCK.UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改. 当我们用UPDLOCK来读取记录时可以对取到的记录加上更新锁,从而 ...

  7. 保存vim的ide环境

    开发周期不是一两天, 要把当前的窗口布局, 命令历史/寄存器历史等保存下来,以便下次编写时快速恢复. 需要保存两个方面的信息: session: 保存窗口的view试图窗口布局, 和全局设置   :m ...

  8. 使用 Python 创建你自己的 Shell(下)

    导读 在上篇中,我们已经创建了一个 shell 主循环.切分了命令输入,以及通过 fork 和 exec 执行命令.在这部分,我们将会解决剩下的问题.首先,cd test_dir2 命令无法修改我们的 ...

  9. 安装ruby

    (这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 过程中有点小曲折,我们leader是技术大牛,现在我生命中多了个超高智商处女座man了,还有一 ...

  10. 原生Android动作

    ACTION_ALL_APPS:打开一个列出所有已安装应用程序的Activity.通常,此操作又启动器处理. ACTION_ANSWER:打开一个处理来电的Activity,通常这个动作是由本地电话拨 ...