LEETCODE 77. Combinations
来源:互联网 发布:球球大作战挂机软件 编辑:程序博客网 时间:2024/06/04 00:32
题目大意
解题思路
用递归的思路很简单,只需要将不重复的数字一次push进当前的vector就行,当vector的大小等于k的时候,就将结果记录下来。
代码
class Solution {public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> resultVec; vector<int> singleVec; getCombs(resultVec, singleVec, 1, k, n); return resultVec; }private: void getCombs(vector<vector<int>> &resVec, vector<int> singleComb, int beginInd, int vecSize, int numSize) { if (singleComb.size() == vecSize) { resVec.push_back(singleComb); return; } for (int i = beginInd; i <= numSize; i++) { singleComb.push_back(i); getCombs(resVec, singleComb, i + 1, vecSize, numSize); singleComb.pop_back(); } }};
记录一个更快更简短的方法
class Solution {public: vector<vector<int>> combine(int n, int k) { vector<vector<int>> result; int i = 0; vector<int> p(k, 0); while (i >= 0) { p[i]++; if (p[i] > n) --i; else if (i == k - 1) result.push_back(p); else { ++i; p[i] = p[i - 1]; } } return result; }};
0 0
- LeetCode 77. Combinations
- [LeetCode]77.Combinations
- LeetCode --- 77. Combinations
- [Leetcode] 77. Combinations
- [leetcode] 77.Combinations
- leetCode 77.Combinations (组合)
- [leetcode] 77. Combinations
- 77. Combinations LeetCode
- <LeetCode OJ> 77. Combinations
- [LeetCode]77. Combinations
- leetcode 77. Combinations
- LeetCode *** 77. Combinations
- leetcode 77. Combinations
- LeetCode 77. Combinations
- LeetCode-77.Combinations
- LeetCode 77.Combinations
- [LeetCode]problem 77. Combinations
- [LeetCode] 77. Combinations
- BitBlts, AlphaBlend, TransparentBlt, 和 StretchBlt 有 GPU 加速
- OpenGL 很细的介绍
- 折半查找法
- 江苏科技大学“FlySpiders”本创小组第二次比赛活动-----1组
- ACM 粗心永远AC不了系列——Demo Day|三维DP问题,机器人寻路,hiho一下第150周
- LEETCODE 77. Combinations
- 读书笔记——《模式识别 Pattern Recognition》
- MySQL 数据类型
- 迭代器介绍
- 第四章 高级特性
- Node.js核心模块
- uva 10340
- 04-树7 二叉搜索树的操作集 (30分)
- 对log工具类的方法封装