Question

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],
]

Solution

Draw out solution tree. Given [4, 2]

            []

      /    /    \    \

     1    2     3    4

    / | \  / \     |

   2   3   4  3    4             4

According to this tree, we can write out solution.

 public class Solution {
public List<List<Integer>> combine(int n, int k) {
if (k > n)
k = n;
List<List<Integer>> result = new ArrayList<List<Integer>>();
dfs(n, k, 1, new ArrayList<Integer>(), result);
return result;
} private void dfs(int n, int k, int start, List<Integer> list, List<List<Integer>> result) {
if (list.size() == k) {
result.add(new ArrayList<Integer>(list));
return;
} for (int i = start; i <= n; i++) {
list.add(i);
dfs(n, k, i + 1, list, result);
list.remove(list.size() - 1);
}
}
}

Conclusion -- start pointer or visited[]

总结一下Backtracking这一类常见问题。

Backtracking一般都是应对穷举来找出所有答案的问题。对于这类问题,我们常常可以画出解空间树。然后遍历解空间树就可以得到答案。

遍历可以用 BFS 也可以DFS。一般来说,用迭代实现的DFS,不用自己维护栈所以是比较方便的做法。如果用BFS,则需要纪录一系列状态值。

在迭代的时候,我们要注意迭代结束之后要回到未迭代时的状态。

1. 如果有list操作,要把list的最后一个element移出

2. 如果有visited[],重新设置该element的状态为未访问过。

对比这些题目 Permutation, PhoneNumber,我们发现有的时候用到了start指针来代替visited[]数组表明该结点都无访问过。

那什么时候用start指针,什么时候用visited[]呢?

Combinations 解答的更多相关文章

  1. Letter Combinations of a Phone Number 解答

    Question Given a digit string, return all possible letter combinations that the number could represe ...

  2. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  3. 【LeetCode题意分析&解答】39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  4. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  5. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. LeetCode OJ 77. Combinations

    题目 Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa ...

  7. LeetCode题目解答

    LeetCode题目解答——Easy部分 Posted on 2014 年 11 月 3 日 by 四火 [Updated on 9/22/2017] 如今回头看来,里面很多做法都不是最佳的,有的从复 ...

  8. LeetCode算法题目解答汇总(转自四火的唠叨)

    LeetCode算法题目解答汇总 本文转自<四火的唠叨> 只要不是特别忙或者特别不方便,最近一直保持着每天做几道算法题的规律,到后来随着难度的增加,每天做的题目越来越少.我的初衷就是练习, ...

  9. Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

随机推荐

  1. Linux文件系统挂载管理

    http://itercast.com/lecture/19 文件系统创建好之后需要挂载到系统中方可使用,windows.Mac系统会自动挂载文件系统,而Linux下一般需要手工挂载或配置系统进行自动 ...

  2. Spiral Matrix 解答

    Question Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in ...

  3. UVA10817--状态压缩DP

    第一次做状态压缩dp..没有思路..看书看明白的,不过看完发现汝哥的做法多算了一些东西,完全可以省去不算.. 用两个集合,s1表示恰好有一个人教的科目,s2表示至少有两个人教的科目.d(i,s1,s2 ...

  4. 【多线程】--生产者消费者模式--synchronized版本

    在实现生产者消费者模式之前,我们先了解一下线程的5种状态:被创建.运行.冻结.消亡.阻塞,如下图: 在Jdk1.5发布之前,我们实现生产者消费者模式一般使用synchronized + while循环 ...

  5. [Hapi.js] Logging with good and good-console

    hapi doesn't ship with logging support baked in. Luckily, hapi's rich plugin ecosystem includes ever ...

  6. UML学习-活动图创建

    活动图(Activity Diagram)可以实现对系统动态行为的建模,主要是将用例细化,即用例内部的细节可以以活动图的方式描述.活动图描述活动的顺序,主要表活动之间的控制流,是内部处理驱动的流程,在 ...

  7. 创建一个简单的配置android编译环境的脚本

    由于有多个Android项目,每个项目配置编译环境时选项都不同,所以尝试写一个sh脚本来完成这个功能.     首先进入bin文件夹,新建一个文件enbuild $ cd ~/bin $ touch ...

  8. 转:ProGuard 常见命令备份

    转: https://my.oschina.net/sunyh/blog/30359 #-dontshrink #不压缩,减少jar的大小一般都压缩掉,为了增加反编译的难度也可以不压缩 #-donto ...

  9. web 前端 shopnc项目 首页分类一开始做前端,我是拒绝的

    看图别说话 经过几小时的折腾 主要还是靠耐心

  10. shell date格式化输出

    1-  echo `date "+%Y-%m-%d %H:%M:%S"` 2014-11-13 15:06:26 2-  echo `date "+%y-%m-%d %H ...