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 ...
随机推荐
- css子元素的margin-top为何会影响父元素
详细内容请点击 这个问题困惑了很久,虽然没有大碍早就摸出来怎么搞定它,但始终不明白原因出在哪里,如果只是IE有问题我也不会太在意,可问题是所有上等浏览器都表现如此,这样叫我怎能安心?今天总算下狠心查出 ...
- ruby学习--block
#当前块 class Block def a_method return yield if block_given? 'no block' end end obj=Block.new puts &qu ...
- asp.net mvc @RenderBody()的问题
在使用.net mvc 母版页布局时如果是进行上中下三块布局的话,那么就会像下面的图那样: 在上面的div 和下面的div之间会出现4cm的间隔, 解决如下: 给包裹@RenderBody()的div ...
- Cocos2d-x移植到WindowsPhone8移植问题-libcurl库移植问题
在Cocos2d-x 3.x最新版本中提供了Windows Phone 8平台移植libcurl库所需要的头文件和库文件.但要在Windows Phone 8平台成功移植libcurl库还是很不容易, ...
- JS数据类型转换
JS 数据类型转换 方法主要有三种 转换函数.强制类型转换.利用js变量弱类型转换. 1. 转换函数: js提供了parseInt()和parseFloat()两个转换函数.前者把值转换成整数,后者把 ...
- 读书笔记-常用设计模式之MVC
1.MVC(Model-View-Controller,模型-视图-控制器)模式是相当古老的设计模式之一,它最早出现在SmallTalk语言中.MVC模式是一种复合设计模式,由“观察者”(Observ ...
- IOS 模仿TableView封装
一.先贴一下未封装的代号,好跟后面的对比 @interface MTHomeDropdown : UIView + (instancetype)dropdown; @property (nonatom ...
- 20141214--C#父类,子类
首要: 子类 包含父类的所有的属性方法 所有子类都可以直接转化成父类类型 当父类类型变量里面存的是某个子类的对象的时候,才能转化成那个子类类型. 父类与子类的装换: Ren r = new Ren() ...
- echarts标准饼图(一)——基本配置demo
echarts标准饼图解读共分为四部分, 一.基本配置demo 二.标题(title)配置 三.提示框(tooltip)配置 四.图例(legend)配置 五.系列列表(series )配置 下面是一 ...
- NodeJs多进程和socket.io通讯-DEMO
一.开启多进程 const os = require('os'); const cp = require('child_process'); const forkList = {}; const fo ...