77. Combinations(medium, backtrack, 重要, 弄了1小时)
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小时)的更多相关文章
- 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 ...
- 39. Combination Sum(medium, backtrack 的经典应用, 重要)
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique c ...
- (效率低下)77. Combinations C++回溯法 组合
https://leetcode.com/problems/combinations/ 沿用78题的思路 class Solution { public: void backTrack(vector& ...
- Leetcode 77, Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 77. Combinations
题目: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For ex ...
- 47. Permutations II(medium, backtrack, 重要, 条件较难思考)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 46. Permutations(medium, backtrack, 重要)
Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] have ...
- 【一天一道LeetCode】#77. Combinations
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [leetcode]77. Combinations组合
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: I ...
随机推荐
- 新概念英语(1-32)A fine day
新概念英语(1-33)A fine day Where is the Jones family? It is a fine day today. There are some clouds in th ...
- gradle入门(1-2)gradle的依赖管理
Gradle支持以下仓库格式: Ivy仓库 Maven仓库 Flat directory仓库 一.添加仓库 1.添加Ivy仓库 1.1.通过URL地址添加一个Ivy仓库 我们可以将以下代码片段加入到b ...
- css(1-1)样式表
CSS Id 和 Class id 和 class 选择器 如果你要在HTML元素中设置CSS样式,你需要在元素中设置"id" 和 "class"选择器. id ...
- SQL查询语句练习
最近在学习SQL嘛,所以各个地方找题目来练手,毕竟现在能离得开数据库么? Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C ...
- shell:bash环境
1.什么是shell shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个是shell脚本. 命令解释器shell的发展史,sh-csh-ksh-tcsh-bash. 2.命令的 ...
- uvalive 3602 DNA Consensus String
https://vjudge.net/problem/UVALive-3602 题意: 给定m个长度均为n的DNA序列,求一个DNA序列,使得它到所有的DNA序列的汉明距离最短,若有多个解则输出字典序 ...
- Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) C. Classroom Watch
http://codeforces.com/contest/876/problem/C 题意: 现在有一个数n,它是由一个数x加上x每一位的数字得到的,现在给出n,要求找出符合条件的每一个x. 思路: ...
- linux(centos)常用命令
原文:https://blog.csdn.net/zhangzhikaixinya/article/details/44538571 1.查看当前所在路径:pwd 2.新建文件夹www:mkdir w ...
- Struts2笔记分享(一)
Struts2概述1.简介Struts就是在Model2的基础上实现的一个MVC框架,它只有一个中心控制器,采用XML定制的转向的URL,采用Action来处理逻辑.2.Struts2的MVC模式MV ...
- 是否可能两个ETH私钥对应同一个地址
原提问在这里. 笔者在使用到neon-js中的私钥生成方法时发现其使用了getRandomValues方法来生成64字符长度的私钥,进而考虑到其随机性,若是调用足够多次,依然有可能生成两个完全一样的私 ...