leetcode 77. Combinations
来源:互联网 发布:淘宝借贷平台 编辑:程序博客网 时间:2024/05/21 19:47
题意
求在区间[1,n]内的k个数的所有组合。
题解
用一个flag数组表示区间内的一个数是否在当前组合内:存在(true)、不存在(false)
代码
class Solution {public: vector<vector<int> > result; void combine_recur(bool flag[], int pos, int n, int k) { if(pos == n) { if(k != 0) return; else if(k == 0) { vector<int> temp; for(int i = 1; i < n; i++) if(flag[i] == true) temp.push_back(i); result.push_back(temp); } } else { flag[pos] = true; combine_recur(flag, pos + 1, n, k - 1); flag[pos] = false; combine_recur(flag, pos + 1, n, k); } } vector<vector<int>> combine(int n, int k) { bool *flag = new bool[n + 1]; fill(flag, flag + n + 1, false); combine_recur(flag, 1, n + 1, k); 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
- MySQL: table、view、trigger、procedure、index (表、主键、UNIQUE Key、列、索引、视图、触发器、存储过程)查看、修改、更新、删除操作
- App测试中ios和Android的区别
- UVA 820 Internet Bandwidth(最大流)
- nginx 隐藏index.php 支持PATHINFO
- oracle存储过程中使用execute immediate执行sql报ora-01031权限不足的问题
- leetcode 77. Combinations
- Unity3D中的Coroutine详解
- HDU 2066.一个人的旅行【最短路径(多次Dijsktra算法)】【4月17】
- socket编程基于IO复用的多客户端服务器
- emacs修改键盘映射
- 安装java,tomcat遇到的问题
- spring 和mybatis整合时 使用context:property-placeholder载不进属性 还报org.springframework.beans.factory.BeanCrea
- Launcher3源码浅析(5.1)--OverviewMode
- 设计模式——单例模式