LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
给出相应的按键,然后要求给出所有可能的字母组合,典型的dfs问题,但是这里的重点在于字典的店里,以及怎么来进行组合,代码如下,原理比较简单,这里就不再赘述了。PS:这题一开始其实没有想明白怎么建立字典,但是实际上通过一个map简单的就可以建立数字与字母之间的关联,代码如下所示:
class Solution {
public:
vector<string> letterCombinations(string digits) {
if(!digits.size())
return ret;
createDic(dic);
dfs(, digits.size(), digits, "");
return ret;
}
void createDic(map<char, vector<char>> & dic)
{
dic[''].push_back('a');
dic[''].push_back('b');
dic[''].push_back('c');
dic[''].push_back('d');
dic[''].push_back('e');
dic[''].push_back('f');
dic[''].push_back('g');
dic[''].push_back('h');
dic[''].push_back('i');
dic[''].push_back('j');
dic[''].push_back('k');
dic[''].push_back('l');
dic[''].push_back('m');
dic[''].push_back('n');
dic[''].push_back('o');
dic[''].push_back('p');
dic[''].push_back('q');
dic[''].push_back('r');
dic[''].push_back('s');
dic[''].push_back('t');
dic[''].push_back('u');
dic[''].push_back('v');
dic[''].push_back('w');
dic[''].push_back('x');
dic[''].push_back('y');
dic[''].push_back('z');
}
void dfs(int dep, int maxDep, string & s, string ans)
{
if(dep == maxDep){
ret.push_back(ans);
return;
}
for(int i = ; i < dic[s[dep]].size(); ++i) //这里的dep应该得到注意
dfs(dep+, maxDep, s, ans + dic[s[dep]][i]) ;
}
private:
map<char, vector<char>> dic;
vector<string> ret;
};
LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)的更多相关文章
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- Hibernate与autoCommit
JDBC 的autoCommit属性 对于每一个 JDBC connection,都有一个autoCommit属性,只有执行commit后,该connection中的操作(statement操作)才会 ...
- zookeeper No route to host
2017-10-12 07:25:59,270 [myid:1] - WARN [QuorumPeer[myid=1]/0:0:0:0:0:0:0:0:2181:QuorumCnxManager@36 ...
- FTP 两种工作模式
主动模式port FTP主动模式:TCP链接客户端访问FTP,客户端会开启一个大于1024的端口N访问FTP的21端口(控制端口),并通过21端口发送port命令与N+1的端口,服务端收到命令后会使用 ...
- openwrt中如何在一个软件包中使能busybox中的工具
答:在软件包的Makefile中定义一个宏Package/package-name/config 举例:笔者自己制作了一个名为hello的软件包,但是这个软件包依赖busybox中的ifdown de ...
- CentOS安装wkhtmltopdf及解决中文支持问题
安装wkhtmltopdf,先下载 wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm yum install -y wkhtmltox-0.12.2.1_linu ...
- windows批量删除ip
cmd下输入如下命令第一步:netsh -c int ip dump >c:\ip.txt在C盘根目录看到一个ip.txt的文件,内容为当前网卡的设置信息,为了能更直观的看清楚IP的设置信息. ...
- LeetCode——largest-rectangle-in-histogram1
Question Given n non-negative integers representing the histogram's bar height where the width of ea ...
- Graph_Master(连通分量_D_Trajan缩点+dfs)
hdu_2242 题目大意:求将一张无向图(n个点,m条边)移除一条边分为不连通两部分,使得两部分的点权和最接近,若无法分为两部分,则输出impossible. 题解:拿到题面还算清晰,就是先tarj ...
- PAT1075. PAT Judge (25)
其中在排名输出上参照了 http://blog.csdn.net/xyzchenzd/article/details/27074665,原先自己写的很繁琐,而且还有一个测试点不通过. #include ...
- 获取远程html
/// <summary> /// 获取远程html /// </summary> /// <param name="url"></par ...