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

做了好几个这种题了,就是 backtrack problem, 1小时做出,但不容易,各种小毛病.

我的程序蠢蠢的建立了一个 vector<int> A;. 其实,只玩弄变量n就行了.

我犯的错误,如下:

start + 1, start++ 和 ++start

自己第一次写的代码是
for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]); // 应把 start+1 改为 ++start, start 应该随 i 变化而变化
backtrack(res, temp, A, len, start + 1);
temp.pop_back();
} e.g.
n = 4, k = 2
[1 2 3 4]
结果应为: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]]
我错误结果为: [[1,2],[1,3],[1,4],[2,2],[2,3],[2,4],[3,2],[3,3],[3,4],[4,2],[4,3],[4,4]]

自个想法,自个代码:

vector<vector<int>> combine(int n, int k) {
vector < vector<int> > res;
vector<int> temp;
vector<int> A;
for (int i = 1; i <= n; i++)
A.push_back(i); backtrack(res, temp, A, k, 0);
return res;
} void backtrack(vector<vector<int> >& res, vector<int>& temp, vector<int>& A,
const int len, int start) {
if (temp.size() == len) {
res.push_back(temp);
return;
} for (int i = start; i < A.size(); i++) {
temp.push_back(A[i]);
// start++;
// 下面血淋淋地体现了
// start + 1, start++ 和 ++start 的巨大不同了
backtrack(res, temp, A, len, ++start);
temp.pop_back();
}
}

77. Combinations(medium, backtrack, 重要, 弄了1小时)的更多相关文章

  1. 216. Combination Sum III(medium, backtrack, 本类问题做的最快的一次)

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  2. 39. Combination Sum(medium, backtrack 的经典应用, 重要)

    Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...

  3. (效率低下)77. Combinations C++回溯法 组合

    https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...

  4. Leetcode 77, Combinations

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

  5. 77. Combinations

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

  6. 47. Permutations II(medium, backtrack, 重要, 条件较难思考)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  7. 46. Permutations(medium, backtrack, 重要)

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

  8. 【一天一道LeetCode】#77. Combinations

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  9. [leetcode]77. Combinations组合

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...

随机推荐

  1. IDE-Ecplise-代码注释 模版 编码规范 配色

    说明: 代码注释主要用于方便代码后期维护,编码规范,增加代码阅读性和维护性.因网上看到的很多博客中片段局多,故整理后重写一篇,方便交流学习. 先看下加过注释模版后的效果. 如上图所示,创建类,方法和继 ...

  2. 实现GridControl行动态改变行字体和背景色

    需求:开发时遇到一个问题, 需要根据GridControl行数据不同,实现不同的效果 在gridView的RowCellStyle的事件中实现,需要的效果 private void gridView1 ...

  3. Java-Maven(七):Eclipse中Maven依赖、聚合、继承特性

    之前通过学习了解,maven集成到eclipse中的如何创建项目,以及maven命令插件在eclipse中安装后的用法.那么接下来我们将会学习一些maven在项目中的一些特性,及如何使用. Maven ...

  4. Spring-cloud (一):Eureka注册中心搭建

    前提 系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源) 项目搭建 新建一个maven项目,创建最简单的那种就好,项目名这里为Eurek ...

  5. Hibernate | Spring JPA | MySQL 使用过程遇到的一些问题

    1. 使用过程 2. 背景 3. 遇到问题 3.1 不指定Hibernate数据库方言,默认SQL生成方式 3.2 抛出异常Hibernate加入了@Transactional事务不会回滚 3.3 H ...

  6. 数轴line

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAq8AAAGaCAYAAAAhPqoeAAAgAElEQVR4nOzdCbh2U/k/8C0NpkgRzZ

  7. Codeforces Round #408 (Div. 2)

    C. Bank Hacking 题目大意:给出一棵n个节点的树,每个节点有一个权值,删掉一个点的代价为当前这个点的权值,并且会使其相邻点和距离为2且中间隔着未被删除的点的点权值加1,现在选一个点开始删 ...

  8. hdu4549(费马小定理 + 快速幂)

    M斐波那契数列F[n]是一种整数数列,它的定义如下: F[0] = a F[1] = b F[n] = F[n-1] * F[n-2] ( n > 1 ) 现在给出a, b, n,你能求出F[n ...

  9. 浅析Linux内核调度

    1.调度器的概述 多任务操作系统分为非抢占式多任务和抢占式多任务.与大多数现代操作系统一样,Linux采用的是抢占式多任务模式.这表示对CPU的占用时间由操作系统决定的,具体为操作系统中的调度器.调度 ...

  10. hdu 5478 (数论)

    ⋅n+b1⋅n−k2+1 = 0 (mod C)(n = 1, 2, 3, ...)  (1<=a, b <C) 1.  当n = 1时, a^(k1+b1) + b = 0 ( mod ...