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 ...
随机推荐
- .NET DLL 保护措施详解(四)各操作系统运行情况
我准备了WEB应用程序及WinForm应用程序,分别在WIN SERVER 2012/2008/2003.Win7/10上实测,以下为实测结果截图: 2012 2008 2003 WIN7 WIN10 ...
- Varnish缓存服务器的搭建配置手册
Varnish缓存服务器的搭建配置手册 1.Varnish官方环境依赖提示 Installing Varnish Cache is as simple as enabling our package ...
- About Restful Web Api Something.
这种轻量级的服务架构目前来说还是比较流行的,比如微信的公众平台的接口开发就是一个很好的案例,提到restful那么他到底是一个什么样的东西? REST(Representational State T ...
- (js有关图片加载问题)dom加载完和onload事件
引用旺旺的话...哈哈哈DOMContentLoaded事件表示页面的DOM结构绘制完成了,这时候外部资源(带src属性的)还没有加载完.而onload事件是等外部资源都加载完了就触发的.$.read ...
- cocos2d-x中Node与Node层级架构
Cocos2d-x采用层级(树形)结构管理场景.层.精灵.菜单.文本.地图和粒子系统等节点(Node)对象.一个场景包含了多个层,一个层又包含多个精灵.菜单.文本.地图和粒子系统等对象.层级结构中的节 ...
- UI1_UITableViewSearchController
// UI1_UITableViewSearchController // // Created by zhangxueming on 15/7/15. // Copyright (c) 2015年 ...
- 4月12日学习笔记——jQuery事件
下面是在 jQuery 中最常使用的 bind()方法举例:$("#testDiv4").bind("click", showMsg); 我们为 id 是 te ...
- HDU1064 第一道JAVA
简单的不能再简单的题目, 不过倒是可以来练练新学的JAVA.. import java.util.Scanner; public class Hello{ public static void mai ...
- linux kernel with param
Linux kernel support pass param to kernel, this params can be assigned at load time by insmod or mod ...
- Visual Studio 2012 使用免费的Team Foundation Service
VS2012提供了在线的TFS服务,免费支持五人小团队,收费情况尚未确定,下面本文演示如何申请和连接在线TFS 服务器. 一.申请TFS服务 首先,打开VS2012,看看是否有团队资源管理器,如果没有 ...