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. bzoj3431 [Usaco2014 Jan]Bessie Slows Down

    Description [Brian Dean, 2014] Bessie the cow is competing in a cross-country skiing event at the wi ...

  2. linux下strace命令详解

    简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...

  3. Linux系统启动流程(2)

    内核设计风格: RedHat, SUSE核心:动态加载 内核模块内核:/lib/modules/“内核版本号命令的目录”/vmlinuz-2.6.32/lib/modules/2.6.32/ RedH ...

  4. 第17讲- UI常用组件之ImageView图片浏览

    第17讲 UI常用组件之ImageView图片浏览 二.图片浏览ImageView ImageView就是一个用来显示图片的视图: ImageView常见属性 常见属性 对应方法 说明 android ...

  5. js实现a标签超链接提交form表单的方法

    <a class="regButton"    id="saveRegister" onclick="document.getElementBy ...

  6. php随机函数

    <?php function generate_password( $length = 6 ) { // 密码字符集,可任意添加你需要的字符 // $chars = 'abcdefghijklm ...

  7. Objective-C(十八、谓语使用及实例说明)——iOS开发基础

    结合之前的学习笔记以及參考<Objective-C编程全解(第三版)>,对Objective-C知识点进行梳理总结.知识点一直在变,仅仅是作为參考,以苹果官方文档为准~ 十八.谓语的使用及 ...

  8. 使用Xshell连接Ubuntu

    使用Xshell连接Ubuntu Xshell是一个安全终端模拟软件,可以进行远程登录.我使用XShell的主要目的是在Windows环境下登录Linux终端进行编码,非常方便.本文简单介绍下它的使用 ...

  9. struts2面试题

    由于找了很久的工作都没有找的,只能四处收集那个面试题的.和看面试题的 还有那个记忆力也不是很好了的,而那些公司面试的时候总会有一个面试题的!   在这里分享给大家(那个本来是想上传文件的,但是找不到的 ...

  10. 更新Android SDK 出错 Failed to rename directory \temp\ToolPackage.old01

    打算更新Android SDK 版本到 Android SDK Tools 20,打开SDK Manager.exe 开始更新,结果安装时弹出错误提示:Failed to rename directo ...