77. Combinations
来源:互联网 发布:五轴编程招聘 编辑:程序博客网 时间:2024/05/16 15:42
Problem:
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],]
Solution:
#include<algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<stdlib.h>
#include<vector>
#include<stack>
using namespace std;
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;
}
};
int main()
{
Solution s;
for (auto i: s.combine(8, 5))
{
for (auto j : i)
{
cout << j;
}
cout << endl;
}
getchar();
return 0;
}
阅读全文
0 0
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 77. Combinations
- 节间通讯(gossip)
- ListView的多条目的适配器
- Python学习 第二天任务 (二:【基于Python编程从入门到实践】第二章 数字和注释 书本及动手试一试)
- Java设计模式-代理模式
- python3之1005.继续(3n+1)猜想 (25分)
- 77. Combinations
- 搭建LNMP环境
- POJ
- InputStreamToString
- mysql(12):基础,ALTER 的语法介绍
- 中本聪比特币论文
- spring mvc防重复提交
- 2017年浙江工业大学大学生程序设计迎新赛热身赛 Iafter与迷宫 牛客
- openwrt源码目录