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 ...
随机推荐
- 保持简单----纪念丹尼斯•里奇(Dennis Ritchie)
http://www.ruanyifeng.com/blog/2011/10/dennis_ritchie.html
- centos配置jdk的环境变量
1.首先呢,centos下的JDK环境配置分两种情况,一直是root用户级别的jdk配置,另一种是其他用户组级别的配置.这里讲解的是root用户级别的配置. 我们已经下载解压好了jdk的目录.如下 2 ...
- php 中处理 websocket
http://www.cnblogs.com/hustskyking/p/websocket-with-php.html 下面我画了一个图演示 client 和 server 之间建立 websock ...
- Python面试题之Python正则表达式re模块
一.Python正则表达式re模块简介 正则表达式,是一门相对通用的语言.简单说就是:用一系列的规则语法,去匹配,查找,替换等操作字符串,以达到对应的目的:此套规则,就是所谓的正则表达式.各个语言都有 ...
- tomcat和apache的区别
1. Apache是web服务器,Tomcat是应用(java)服务器(也可作web服务器),它只是一个servlet容器,是Apache的扩展. 2. Apache和Tomcat都可以做为独立的we ...
- 20145309《Java程序设计》第七周学习总结
教材学习内容总结 第13章 时间与日期 13.1 认识时间与日期 13.1.1 时间的度量 格林威治时间(GMT) 世界时(UT) 国际原子时(TAI) 世界协调时间(UTC) Unix时间:Unix ...
- Oh My Fish! 让你的 Shell 漂亮起来
安装 Oh My Fish 安装 omf 很简单.你要做的只是在你的 Fish shell 中运行下面的命令. curl -L https://get.oh-my.fish | fish 一旦安装完成 ...
- MongoTemplate WriteResult acknowledged=false 的问题
今天使用 MongoTemplate 的 update 操作时,发现 WriteResult 的 acknowledged 一直为 false ,个人首先想到可能时java驱动版本不对,在更换好对应版 ...
- python中的上下文管理器
刚刚看了vamei大神的上下文管理器博客,理解如下: 其实我自己经常用到上下文管理器,尤其是在打开文件的时候,如果自己比较懒,不想手工打上f.close(),使用上下文管理器就ok拉. 上下文管理器就 ...
- AtCoder Grand Round 012B Splatter Painting
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...