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(数字字母组合)的更多相关文章

  1. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. 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 ...

  3. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  5. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. 【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 ...

  7. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  10. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

随机推荐

  1. Ubuntu16.04中用et对jmeter生成的数据统计成图表

    在Ubuntu系统中,用ctrl+Alt+t 打开终端: 输入et,即打开wps: 整理需要形成图表的数据,如: 用excel生成图表,如下: 表得出的性能图表,方法: 1.工具栏中选择插入——二维折 ...

  2. JSON.stringify without quotes on properties?

    https://stackoverflow.com/questions/11233498/json-stringify-without-quotes-on-properties/11233515 va ...

  3. 【分库分表】sharding-jdbc—解决的问题

    一.遇到的问题 随着互联网技术和业务规模的发展,单个db的表里数据越来越多,sql的优化已经作用不明显或解决不了问题了,这时系统的瓶颈就是单个db了(或单table数据太大).这时候就涉及到分库分表的 ...

  4. Nginx将不同IP的请求分发到不同的WEB服务器

    server { listen ; server_name localhost; large_client_header_buffers 16k; client_max_body_size 300m; ...

  5. 一.复习GCC编译器的用法

    1.复习GCC编译器的用法 欲善其工,那么要先利其器.在这个C语言巩固与提高的阶段中,如果想要更好的达成预期目标,首先就要熟练掌握GCC编译器的用法.以下是GCC相关知识: GCC使用语法 gcc 选 ...

  6. 20145331实验四 "Android开发基础"

    20145331实验四 "Android开发基础" 程序设计过程 实验内容 ·安装Android Studio ·运行安卓AVD模拟器 ·使用安卓运行出虚拟手机并显示HelloWo ...

  7. 20145335郝昊《Java程序设计》课程总结

    20145335郝昊<Java程序设计>课程总结 1.读书笔记汇总 第一周学习总结:http://www.cnblogs.com/20145335hh/p/5244638.html 第二周 ...

  8. MySQL修改max_allowed_packet

    因mysql从库报错Last_IO_Error: Got a packet bigger than 'max_allowed_packet' bytes mysql> show slave st ...

  9. Linux系统CentOS使用yum方式安装指定版本的PHP 添加yum源 从PHP5.3升级到5.4/5.5/5.6

    默认的版本太低了,手动安装有一些麻烦,想采用Yum安装的可以使用下面的方案: 首先删除旧版本的PHP, 通过yum list installed | grep php可以查看所有已安装的php软件 使 ...

  10. nodeJs和JavaScript的异同

    JavaScript组成:ECMAScript(定义这门语言的基础,比如语法.数据类型.结构以及一些内置对象等).DOM(基于ECMASCRIPT,扩展出来的用于操作页面元素的方法).BOM(基于EC ...