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 ...
随机推荐
- Python大婶博客汇总
Python大神金星 博客:http://www.cnblogs.com/jin-xin/articles/7459977.html
- python列表很聪明,支持负数索引
python列表很聪明,支持负数索引
- 使用 vue-i18n 切换中英文
兼容性: 支持 Vue.js 2.x 以上版本 安装方法:(此处只演示 npm) npm install vue-i18n 使用方法: 1.在 main.js 中引入 vue-i18n (前提是要先引 ...
- JS字符串和数组常用方法
1.indexOf() – 返回字符串中一个字符第一处出现的索引,接收2个参数:要查找的字符,从哪个位置开始查找:.lastIndexOf()--返回字符串中某一个字符最后一次出现的索引值. 如果没有 ...
- YII2框架下使用PHPExcel导出柱状图
导出结果: 首先,到官网下载PHPExcel插件包,下载后文件夹如下: 将Classes文件夹放入到项目公共方法内. 新建控制器(访问导出的方法):EntryandexitController < ...
- [机器学习Lesson4]多元线性回归
1. 多元线性回归定义 多元线性回归也被称为多元线性回归. 我们现在介绍方程的符号,我们可以有任意数量的输入变量. 这些多个特征的假设函数的多变量形式如下: hθ(x)=θ0+θ1x1+θ2x2+θ3 ...
- [LeetCode] Maximum Average Subarray I 子数组的最大平均值
Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...
- JavaScript之Promise对象
含义 Promise 是异步编程的一种解决方案,比传统的解决方案--回调函数和事件--更合理和更强大.它由社区最早提出和实现,ES6 将其写进了语言标准,统一了用法,原生提供了 Promise 对象. ...
- STM32 - GPIO
买了一个STM32F4的开发板,想把上面的东西重新学一下 快速过: 一.GPIO控制 void GPIO_DeInit(GPIO_TypeDef* GPIOx); //把某一个IO口恢复到默认值 /* ...
- [测试题]数组(array)
Description Input Output Sample Input1 3 2 75 4 2 Sample Output1 999999732 Sample Explanation1 Sampl ...