Careercup - Facebook面试题 - 5765850736885760
2014-05-02 10:07
原题:
Mapping
'' = 'A','B','C'
'' = 'D','E','F'
...
'' = input:
output :ouput = [AAD, BBD, CCD, AAE, AAF, BBE, BBF, CCE, CCF]
题目:电话的数字按键和字母有个映射关系,给定一串数字,请给出所有可能的字符映射方式。
解法:此人也不给个描述,搞的下面一堆人来猜题意。这个题目的意思是说,对于给定的数字串,有多少种不同的映射方式。像“112” -> “ABD”这样是不允许的,因为“1”只能表示一种字母,不能同时表示“A”和“B”。
代码:
// http://www.careercup.com/question?id=5765850736885760
#include <iostream>
#include <string>
#include <vector>
using namespace std; void DFS(int idx, vector<pair<int, char> > &dict, vector<string> &result, const string &s)
{
int i; if (idx == (int)dict.size()) {
static char m[]; for (i = ; i < (int)dict.size(); ++i) {
m[dict[i].first] = dict[i].second;
} static string res; res.clear();
for (i = ; i < (int)s.length(); ++i) {
res.push_back(m[s[i] - '']);
} result.push_back(res);
return;
} switch(dict[idx].first) {
case :
case :
case :
case :
case :
case :
case :
case :
for (i = ; i < ; ++i) {
dict[idx].second = (dict[idx].first * + i) + 'A';
DFS(idx + , dict, result, s);
}
break;
case :
for (i = ; i < ; ++i) {
dict[idx].second = (dict[idx].first * + i) + 'A';
DFS(idx + , dict, result, s);
}
break;
}
} int main()
{
int i;
string s;
vector<pair<int, char> > dict;
vector<string> result;
int a[]; while (cin >> s) {
for (i = ; i < ; ++i) {
a[i] = ;
}
for (i = ; i < (int)s.length(); ++i) {
a[s[i] - ''] = ;
} for (i = ; i < ; ++i) {
if (a[i]) {
dict.push_back(make_pair(i, '\0'));
}
} DFS(, dict, result, s);
cout << "{" << endl;
for (i = ; i < (int)result.size(); ++i) {
cout << " " << result[i] << endl;
}
cout << "}" << endl; dict.clear();
result.clear();
} return ;
}
Careercup - Facebook面试题 - 5765850736885760的更多相关文章
- 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面试题 - 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 ...
- Careercup - Facebook面试题 - 5188884744896512
2014-05-02 07:18 题目链接 原题: boolean isBST(const Node* node) { // return true iff the tree with root 'n ...
随机推荐
- 限额类费用报销单N+1原则
--添加通过自定义档案列表编码及档案编码查询主键 select bd_defdoc.pk_defdoc as defdoc --查询限额类费用类型主键 from bd_defdoc, bd_defdo ...
- ebay的api的开发技术笔记
使用eBay API基本步骤介绍 要开始使用eBay API,需要如下基本步骤: 1. 注册开发帐号: https://developer.ebay.com/join/Default.aspx ...
- UI5_HomeWork
// // View.h // UI5_HomeWork // // Created by zhangxueming on 15/7/2. // Copyright (c) 2015年 zhangxu ...
- Vim保存文件命令 ":wq" 与 ":x" 的区别
CSDN转载 [1] Vim是Unix/Linux系统最常用的编辑器之一,在保存文件时,我通常选择":wq",因为最开始学习vim的时候,就只记住了几个常用的命令:也没有细究命令的 ...
- java和javascript中this区别的浅探讨
今天在学习javascript的时候碰到了this,感觉它跟java里的有点不一样.然后上网查了一下,参考了这篇文章,JavaScript中this关键字详解,发现它们之间的区别主要是这样: java ...
- 《DNS服务缓存的建立》RHEL6
安装dns软件包: 启动dns服务器 配置本地服务器dns域名解析: 查看dns的服务的端口是否打开: 解析百度测试: 安装dns缓存包: 再次访问解析百度
- android Timer and TImerTask
android Timer and TImerTask Caused by: java.lang.IllegalStateException: TimerTask is scheduled alrea ...
- Linux C 程序 线程(18)
线程控制 1.线程与进程的关系 线程:计算机中独立运行的最小单位. 在用户角度:多个线程是同时执行的. 操作系统角度:各个线程交替执行 以上只针对单核CPU的情况 在多核CPU主机上,多个线程可以同时 ...
- 打造自己的3D全景漫游
three.js 示例: 打造H5里的"3D全景漫游"秘籍 - 腾讯ISUX QQ物联星球计划 通过pano2vr直接将鱼眼全景图生成立体空间的六个面:也可通过Photos ...
- 【Qt】Qt之自定义界面(添加自定义标题栏)【转】
简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...