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的题目,注意因为没有顺序,所以为了避免重复的一些解,我们只考虑递增的解。下一个排列题就不一样了。

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

SLOUTION 2:

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

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

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. javaweb项目打成war包

    进入项目文件 jar -cvf newsisAP.war *

  2. eclipse中java项目转成Web项目

    1.找到项目目录下的.project文件 2.编辑.project文件,找到<natures>...</natures> 3.2中找到的结点中加下面的的代码 <natur ...

  3. spring mvc 图片上传,图片压缩、跨域解决、 按天生成文件夹 ,删除,限制为图片代码等相关配置

    spring mvc 图片上传,跨域解决 按天生成文件夹 ,删除,限制为图片代码,等相关配置 fs.root=data/ #fs.root=/home/dev/fs/ #fs.root=D:/fs/ ...

  4. ASP.NET 性能监控和优化入门

    关键要点: 只有与应用指标相关联,基础设施指标才能最大发挥作用. 高效性能优化的关键在于性能数据. 一些APM工具为ASP.NET提供了开箱即用的支持,这样入门使用ASP.NET仅需最小限度的初始设置 ...

  5. 判断URL是否支持断点续传?

    #python #xiaodeng #判断URL是否支持断点续传? import urllib2 req = urllib2.Request('http://ftp.ubuntu.com/') req ...

  6. iOS-启动动态页跳过设计思路

    概述 根据UIBezierPath和CAShapeLayer自定义倒计时进度条,适用于app启动的时候设置一个倒计时关闭启动页面.可以设置进度条颜色,填充颜色,进度条宽度以及点击事件等. 详细 代码下 ...

  7. ASP.NET 加入返回参数ReturnValue

    说明:很多时候,在DBHelper函数中,都能看到以下的代码: cmd.Parameters.Add(, ParameterDirection.ReturnValue, , , string.Empt ...

  8. HashTable、List、ArrayList的经典使用和相互转换

    1.添加引用 using System.Collections; 2.创建并添加数据 Hashtable hs = new Hashtable(); hs.Add("Name1", ...

  9. 教你动手做一个 iOS 越狱 app

    前言 俗话说得好, 万事开头难. 仅仅是上图一个如此简单地不能再简单的小app, 其实都不算是app, 只是注入了一段代码进系统中, 等到特定的函数方法调用的时候就会被我们hook掉, 执行我们写的代 ...

  10. nginx实战六

    Nginx错误日志 https://coding.net/u/aminglinux/p/nginx/git/blob/master/log/error.md Nginx错误日志平时不用太关注,但是一旦 ...