Careercup - Facebook面试题 - 6204973461274624
2014-05-02 02:28
原题:
I/P: N, k O/P: all subset of N with exactly K elements. eg: I/p: N = , K =
O/p:
题目:从N个数中选K个,输出所有选法。
解法:递归解决。
代码:
// http://www.careercup.com/question?id=6204973461274624
#include <vector>
using namespace std; class Solution {
public:
void findKSubsets(int n, int k, vector<vector<int> > &result) {
if (n <= ) {
return;
}
if (k < || k > n) {
return;
} vector<int> subset; DFS(, , subset, result, n, k);
}
private:
void DFS(int in, int ik, vector<int> &subset,
vector<vector<int> > &result,
const int &n, const int &k) {
if (ik == k) {
result.push_back(subset);
return;
} int i;
for (i = in + ; i <= n - (k - ik) + ; ++i) {
subset.push_back(i);
DFS(i, ik + , subset, result, n, k);
subset.pop_back();
}
};
}; int main()
{
vector<vector<int> > result;
int n, k;
int i, j;
Solution sol; while (scanf("%d%d", &n, &k) == && (n > && k > )) {
sol.findKSubsets(n, k, result);
printf("{\n");
for (i = ; i < (int)result.size(); ++i) {
printf(" {");
for (j = ; j < (int)result[i].size(); ++j) {
printf((j ? ", %d" : "%d"), result[i][j]);
}
printf("}\n");
}
printf("}\n"); for (i = ; i < (int)result.size(); ++i) {
result[i].clear();
}
result.clear();
} return ;
}
Careercup - Facebook面试题 - 6204973461274624的更多相关文章
- Careercup - Facebook面试题 - 6026101998485504
2014-05-02 10:47 题目链接 原题: Given an unordered array of positive integers, create an algorithm that ma ...
- Careercup - Facebook面试题 - 5344154741637120
2014-05-02 10:40 题目链接 原题: Sink Zero in Binary Tree. Swap zero value of a node with non-zero value of ...
- Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07 题目链接 原题: Mapping ' = 'A','B','C' ' = 'D','E','F' ... ' = input: output :ouput = [AA ...
- Careercup - Facebook面试题 - 5733320654585856
2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- Careercup - Facebook面试题 - 6321181669982208
2014-05-02 09:40 题目链接 原题: Given a number N, write a program that returns all possible combinations o ...
- Careercup - Facebook面试题 - 5177378863054848
2014-05-02 08:29 题目链接 原题: Write a function for retrieving the total number of substring palindromes. ...
- Careercup - Facebook面试题 - 4907555595747328
2014-05-02 07:49 题目链接 原题: Given a set of n points (coordinate in 2d plane) within a rectangular spac ...
- Careercup - Facebook面试题 - 5435439490007040
2014-05-02 07:37 题目链接 原题: // merge sorted arrays 'a' and 'b', each with 'length' elements, // in-pla ...
随机推荐
- PHP面向对象之将数据库的查询结果序列化成json格式
<?php class link_mysql{ private $host,$uid,$pwd,$db,$link,$res; function link_mysql($_host,$_uid, ...
- 二十、Android -- SDcard文件读取和保存
背景 一些东西可以 ...
- C# tostring 格式化输出 (转)
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- linux中Apache更Nginx环境配置教程
想把Apache2.2换Nginx已经有些日子,今天给动手了.找了个稳定版本:1.4.1 http://nginx.org/download/nginx-1.4.1.zip 配置很简单,就是ngin ...
- Ruby on Raisl应用(一):在Rails上配置Mongoid+Mongodb
一. 概述 最近考虑用ruby on rails 搭建一套Blog系统,前端考虑用Bootstrap,数据库用Mongodb.由于之前没有相关应用经验.先记录下整个项目过程. 现有资源: Mac 笔记 ...
- Objective-C设计模式——原型Prototype(对象创建)
1.原型 原型设计模式所谓原型设计模式,其实就是对象复制,这个特性在所有语言基本上都是存在的. 我们知道在OC中,对象赋值其实是对对象的引用复制,其实就是相当于C语言中的指针.创建了一个新的变量,但是 ...
- iOS NSDate与NSString之间的相互转换
假如我们需要把当前的时间当成一个字符串作为一张图片的名字的话,就需要把当前的时间NSDate类型的数据转换成NSString类型. 又或者在网络请求的时候,我们在网络上的到时间是一个字符串但是在本地就 ...
- ajax请求简写
<script type="text/javascript"> function changle() { $.post( "SendMail", / ...
- eclipse maven spring +spring mvc mybatis
http://yuanmomo.net/archives/449 http://www.tuicool.com/articles/feqUJz http://wenku.baidu.com/link? ...
- trident 序列号问题
在使用Storm的trident做流计算开发时,遇到一个诡异的问题: 我继承IPartitionedTridentSpout或者IOpaquePartitionedTridentSpout接口做事务型 ...