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. C++中const对象和非const对象调用成员函数问题

    一.类MyClass 二.主函数调用 三.结果

  2. Node.js初探之实现能向前台返回东西的简单服务器

    nodejs nodejs文件就是一个简单的js文件. 在shell中运行 Step 1. 打开终端,进入这个js文件所在目#录 Step 2. 用 'node 文件名.js' 命令运行它即可. 用n ...

  3. SourceTree 03 - 跳过账号登录直接进入主界面

    SourceTree系列第1篇 SourceTree 01 - git 客户端介绍(http://www.cnblogs.com/geaosu/p/8807666.html) SourceTree系列 ...

  4. Hive函数:GROUPING SETS,GROUPING__ID,CUBE,ROLLUP

    参考:lxw大数据田地:http://lxw1234.com/archives/2015/04/193.htm 数据准备: CREATE EXTERNAL TABLE test_data ( mont ...

  5. Linux下C语言socket通信实现发送读取的文件内容--简单实现代码

    本次代码涉及到的内容:socket通讯,文件读取 读取的文件以及文件位置: 要读取的文件和c文件在同一个目录下.客户端(client)读取的是123.xml,服务端(server)读取的是23.xml ...

  6. beautiful soup

    beautiful soup是一个可以从html或者xml文件中提取数据的python库,它能够通过你喜欢的转换器实现惯用的文档导航.查找.修改文档的方式. beautiful soup 会帮你节省数 ...

  7. winform 如何加载Url图像(图像)

    解决方法: 1pictureBox1.Image = Image.FromStream(System.Net.WebRequest.Create(http://www.baidu/new.gif ). ...

  8. 在python后台如何将客户端提交的form表单数据提取出来?

    1.获取客户端提交的表达数据,数据类型为ImmutableMultiDictformData = request.form2.将提取的数据转化成字典formDict = formData.to_dic ...

  9. 有些ES6方法极简,但是性能不够好

    So,也许你觉得ES6让你视野大开,但是并不是性能也能跟得上~ 首先,让我们先来一个简单的性能测试: 数组去重 es5写法: function delSame(arr){ var n = []; ; ...

  10. Flask博客开发——登录验证码

    这部分为Flask博客的登录页面加个验证码.使用了PIL模块生成验证码图片,并通过Flask的session机制,进行验证码验证. 1.生成验证码 使用string模块:string.ascii_le ...