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. SpringCloud的应用发布(三)vmvare+linux,xftp,xshell连接linux失败

    Vmvare内的linux虚拟机已经启动,但是 xftp和xshell连接不上? 环境信息:子网 192.168.136.* linux ip:192.168.136.100 一.核对linux的ip ...

  2. HTML的水平居中和垂直居中解决方案

    水平居中:给div设置一个宽度,然后添加margin:0 auto属性 div{ width:200px; margin:0 auto; } 让绝对定位的div居中 div { position: a ...

  3. bs4解析要获取被注掉的部分需先将注释符号去掉

    <div class="xzcf-content"> <div id="sfxz"> <div class="main- ...

  4. 使用vue-cli快速搭建大型单页面应用开发环境

    工作环境:terminal,已经全局安装了vue(可使用npm install -g vue) 全局安装vue-cli npm install -g vue-cli 创建一个基于webpack模板的项 ...

  5. 关于OpenAuth.Net被攻击的感想

    距离上次写博客应该是1年多以前的事情了,看过我博客的人都知道,我从来不在博客园发技术无关的贴子,除了上次离职.但这次我是实在忍不住了. 今天我个人开源项目OpenAuth.Net发布了最新版(有兴趣戳 ...

  6. MySQL获取XML格式数据

    通过再调用mysql工具时使用--xml选项: C:\Users\wang>mysql -uroot -p --xml mydb Enter password: ******** Welcome ...

  7. 同主机下Docker+nginx+tomcat负载均衡集群搭建

    想用Docker模拟一下nginx+tomcat集群部署,今天折腾了一天,遇坑无数,终于在午夜即将到来之际将整个流程走通,借本文希望给同样遇到类似问题的小伙伴们留点线索. 主机环境是CentOS 7, ...

  8. C# 传统四舍五入保留两位小数(网上流传好多错误的版本)

    关于C#里面的Math.Round,很多人都会用到,而且以为是四舍五入,其实不是这样的: C#里面的Math.Round是符合IEEE标准的“四舍五入”,其实是五舍六入. 网上好多流传的下面这种方式实 ...

  9. openfire彻底卸载的方法

    最近百度找openfire彻底卸载的方法,很多都是三句命令行的答案.但是那三句真的无法完全卸载 终于从openfire官网找到了卸载的命令 终端执行下面的命令 sudo rm -rf /usr/loc ...

  10. 【Swift】ios开发中巧用 description 打印对象时,打印对象的属性

    ios开发中我们打印对象的时候,会直接输出对象地址,这样不方便我们开发.我们可以 巧用 description 打印对象时,输出对象的属性 在oc中直接重写即可.swift中需要遵守Printable ...