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. context:exclude-filter 与 context:include-filter 转

    context:exclude-filter 与 context:include-filter 转 1 在主容器中(applicationContext.xml),将Controller的注解打消掉 ...

  2. 【bzoj3150】 cqoi2013—新Nim游戏

    www.lydsy.com/JudgeOnline/problem.php?id=3105 (题目链接) 题意 在第一个回合中,第一个游戏者可以直接拿走若干个整堆的火柴.可以一堆都不拿,但不可以全部拿 ...

  3. POJ1201 Intervals

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  4. bzoj3555 企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1640  Solved: 613 Description P ...

  5. Android基础类之BaseAdapter

    转:http://www.cnblogs.com/mandroid/archive/2011/04/05/2005525.html Android基础类之BaseAdapter BaseAdapter ...

  6. excel公式处理成绩表

    一共有2个需求: 1.平均分:所有每个人的成绩/29;及格率:60分的/29;优秀率:80分/29 2.对总分进行排序,并在另一列中生成排名 平均分:=(c3+c4+.......c31)/29 及格 ...

  7. linux kernel thread(Daemons)

    内核线程是直接由内核本身启动的进程.内核线程实际上是将内核函数委托给独立的进程,与系统中其他进程“并行”执行(实际上,也并行于内核自身的执行),内核线程经常被称为内核“守护进程”.它们主要用于执行下列 ...

  8. Visual Studio Online Integrations-Planning

              原文:http://www.visualstudio.com/zh-cn/explore/vso-integrations-directory-vs

  9. java 打包过程及如何使用第三方jar包

    地址:http://wenku.baidu.com/view/44a1bbed81c758f5f61f6779.html或者 http://wenku.it168.com/d_000575231.sh ...

  10. linux下syscall函数,SYS_gettid,SYS_tgkill

    出处:http://blog.chinaunix.net/uid-28458801-id-4630215.html     linux下syscall函数,SYS_gettid,SYS_tgkill  ...