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 ...
随机推荐
- Backbone Events 源码笔记
用了backbone一段时间了,做一些笔记和总结,看的源码是1.12 backbone有events,model,collection,histoty,router,view这些模块,其中events ...
- 跟我学习dubbo-使用Maven构建Dubbo服务的可执行jar包(4)
Dubbo服务的运行方式: 1.使用Servlet容器运行(Tomcat.Jetty等)----不可取 缺点:增加复杂性(端口.管理) 浪费资源(内存) 官方:服务容器是一个standalone的启动 ...
- Ubuntu系统下载工具的推荐
源 起 大家在上手一段时间Ubuntu系统后,可能突然想起最近新出了一些电影想要下载来看看,但如果用Wine运行迅雷,不是没反应就是启动后也不能下载,针对这个问题,根据我的使用体验推荐大家两款Ubun ...
- UI1_UITouch
// // ViewController.m // UI1_UITouch // // Created by zhangxueming on 15/7/9. // Copyright (c) 2015 ...
- php连接到数据库操作
<?php $result = mysql_query($sql); while($row = mysql_fetch_array($result)) { ?> 要写的内容代码,比如说Ht ...
- MiZ702学习笔记12——封装一个普通的VGA IP
还记得<MiZ702学习笔记(番外篇)--纯PL VGA驱动>这篇文章中,用verilog写了一个VGA驱动.我们今天要介绍的就是将这个工程打包成一个普通的IP,目的是为后面的一篇文章做个 ...
- 理解JavaScript设计模式与开发应用中发布-订阅模式的最终版代码
最近拜读了曾探所著的<JavaScript设计模式与开发应用>一书,在读到发布-订阅模式一章时,作者不仅给出了基本模式的通用版本的发布-订阅模式的代码,最后还做出了扩展,给该模式增加了离线 ...
- [javascript|基本概念|Underfined]学习笔记
Underfined类型的值:underfined(只有一个) 1/声明未初始化 e.g.:var msg;-->msg == underfined:true 2/申明并值初始化为underfi ...
- <img>元素底部为何有空白?
原因: 图片文字等inline元素默认是和父级元素的baseline对齐的,即:vertical-align 的默认值是 baseline:而baseline又和父级底边bottom有一定距离: im ...
- 7款超酷HTML5 3D动画精选应用及源码
对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...