[LeetCode] Combinations
来源:互联网 发布:游戏开发和java工资 编辑:程序博客网 时间:2024/05/19 22:02
Given two integers n and k, return all possible combinations ofk 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],]问题描述:给定两个整数n和k,返回1~n中的k个整数的所有集合。这直接用到了前面http://blog.csdn.net/luofengmacheng/article/details/14229951中的selectn()函数。class Solution {public: vector<vector<int> > selectn(vector<int> s, int n) { if(s.size() <= n) return vector<vector<int> >(1, s); if(n == 0) return vector<vector<int> >(); if(n == 1) { vector<vector<int> > vec; vector<int> ivec; for(vector<int>::iterator iter = s.begin(); iter != s.end(); ++iter) { ivec.push_back(*iter); vec.push_back(ivec); ivec.clear(); } return vec; } int first_elem = s.front(); s.erase(s.begin()); vector<vector<int> > vec1 = selectn(s, n - 1); vector<vector<int> > vec2 = selectn(s, n); for(vector<vector<int> >::iterator iter = vec1.begin(); iter != vec1.end(); ++iter) { (*iter).insert((*iter).begin(), first_elem); vec2.push_back(*iter); } return vec2; } vector<vector<int> > combine(int n, int k) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector<int> s; int i = 1; for(i = 1; i <= n; ++i) { s.push_back(i); } return selectn(s, k); }};
- LeetCode: Combinations
- LeetCode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [LeetCode] Combinations
- Leetcode: Combinations
- LeetCode Combinations
- [Leetcode] Combinations
- [Leetcode] Combinations
- [leetcode] Combinations
- [LeetCode]Combinations
- [leetcode]Combinations
- LeetCode-Combinations
- [leetcode] Combinations
- LeetCode - Combinations
- 【Leetcode】Combinations
- Leetcode: Combinations
- 【leetcode】Combinations
- JVM、JDK、JRE的区别
- 面试问题系列:一致性哈希算法(consistent hashing)
- 《程序员》 -- 宝马中国技术中心主管Carsten Isert:走近 “智能汽车”
- 阿里去IOE化
- 10个以内数的最大值与最小值及其坐标
- [LeetCode] Combinations
- Ubuntu下的android开发环境配置
- Oracle 表分区
- Java String.indexOf(String str)方法
- 重学计划
- JDK中设计模式
- 为哈index fast full scan与full table scan的plan里显示的Bytes一样
- data:image/png;base64是什么
- 点击Win7任务栏资源管理器图标默认打开“计算机”而不是“库”