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

SLOUTION 1:

一段时间不写真心容易不记得。

这是一道典型的模板题。以下是模板写法。

1. 必须时刻注意,for循环里 是i+1不是Index+1,这个老是会忘记。我们当前取过后,应该是处理下一个位置。

2. start 应该是从1开始。这个是这题的特别情况,一般idnex是0开始。

3. combination的题目,注意因为没有顺序,所以为了避免重复的一些解,我们只考虑递增的解。下一个排列题就不一样了。

 // 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
// 的组合。
public void dfs(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
if (path.size() == k) {
ret.add(new ArrayList<Integer>(path));
return;
} // 注意这里的条件i <= n 取n也是合法的!
// Example:
for (int i = start; i <= n; i++) {
path.add(i); // 注意,最后一个参数是i + 1,不是start + 1!!
dfs(n, k, path, ret, i + 1);
path.remove(path.size() - 1);
}
}

SLOUTION 2:

为了优化,在for 中加了一个判断,及时终止。i <= (n - k + 1)

 // 注意,因为求的是组合,所以我们要考虑一下顺序问题,只需要考虑升序。这样的话就不会有重复的
// 的组合。
public void dfs2(int n, int k, List<Integer> path, List<List<Integer>> ret, int start) {
if (0 == k) {
ret.add(new ArrayList<Integer>(path));
return;
} // 注意这里的条件i <= n 取n也是合法的!
// Example:
for (int i = start; i <= (n - k + 1); i++) {
path.add(i); // 注意,最后一个参数是i + 1,不是start + 1!!
dfs(n, k - 1, path, ret, i + 1);
path.remove(path.size() - 1);
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/combination/Combine_1203.java

LeetCode: Combinations 解题报告的更多相关文章

  1. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  2. 【LeetCode】77. Combinations 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  3. leetcode—Palindrome 解题报告

    1.题目描述 Given a string s, partition s such that every substring of the partition is a palindrome. Ret ...

  4. LeetCode C++ 解题报告

    自己做得LeetCode的题解,使用C++语言. 说明:大多数自己做得,部分参考别人的思路,仅供参考; GitHub地址:https://github.com/amazingyyc/The-Solut ...

  5. C++版 - 剑指offer之面试题37:两个链表的第一个公共结点[LeetCode 160] 解题报告

    剑指offer之面试题37 两个链表的第一个公共结点 提交网址: http://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?t ...

  6. LeetCode: Subsets 解题报告

    Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset ...

  7. LeetCode: Triangle 解题报告

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  8. LeetCode: isSameTree1 解题报告

    isSameTree1 Given two binary trees, write a function to check if they are equal or not. Two binary t ...

  9. LeetCode: solveSudoku 解题报告

    Sudoku SolverWrite a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are in ...

随机推荐

  1. glob 模块

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #glob 模块 #http://python.jobbole.com/81552/ #查找文件只用到三个匹配 ...

  2. 保护HTTP的安全

    #如果没有严格的限制访问的权限,公司放在服务器上的重要文档就存在隐患,web需要有一些安全的http形式: #安全方法: #基本认证.摘要认证.报文完整性检查都是一些轻量级的方法,但还不够强大,下面介 ...

  3. set names utf8 【mysql导入中文乱码问题解决】

    由于工作的关系需要经常导入一些sql脚本,但是经常会遇到导入中文出现乱码的问题,搞得自己每次导入sql脚本时都很害怕,自己也上网找了一些资料,尝 试了许多办法,有些方法可以正常导入中文,有些办法却不行 ...

  4. 500 OOPS: chroot

    FTP登录时报错: 1.500 OOPS: chroot 解决方法:关闭SElinux 2.500 OOPS: vsftpd: refusing to run with writable root i ...

  5. 微信小程序的POST和GET请求方式的header区别

    1.post请求: wx.request({ url: 'https://m.***.com/index.php/Home/Xiaoxxf/make_order', header: { "C ...

  6. Android学习系列(18)--App工程结构搭建

     本文算是一篇漫谈,谈一谈关于Android开发中工程初始化的时候如何在初期我们就能搭建一个好的架构.      关于android架构,因为手机的限制,目前我觉得也确实没什么大谈特谈的,但是从开发的 ...

  7. SQL Server数据库命名规范

    良好的命名习惯是一种美德,下面是个人整理的数据库命名规范: 数据库命名规范:  1. 数据库名:          1.1)用产品或项目的名字命名:         1.2)Pascal Case,如 ...

  8. python学习笔记013——内置函数dir()

    1 描述 dir() 函数 不带参数时,返回当前范围内的变量.方法和定义的类型列表: 带参数时,返回参数的属性.方法列表. 如果参数包含方法__dir__(),该方法将被调用. 如果参数不包含__di ...

  9. configure: error: png.h not found.

    PHP的时候提示这个错误 configure: error: png.h not found.,这个是选择安装GD模块才会出现的错误,详细错误信息如下 If configure fails try - ...

  10. Error_GL_总账日记账过账失败