problem:

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"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

thinking:

(1)满足要求的数字为2~9。

(2)用一个array保存数字相应的字符,再用dfs枚举全部解

code:

class Solution {
private:
map<char, vector<char> > dict;
vector<string> ret;
public:
void createDict()
{
dict.clear();
dict['2'].push_back('a');
dict['2'].push_back('b');
dict['2'].push_back('c');
dict['3'].push_back('d');
dict['3'].push_back('e');
dict['3'].push_back('f');
dict['4'].push_back('g');
dict['4'].push_back('h');
dict['4'].push_back('i');
dict['5'].push_back('j');
dict['5'].push_back('k');
dict['5'].push_back('l');
dict['6'].push_back('m');
dict['6'].push_back('n');
dict['6'].push_back('o');
dict['7'].push_back('p');
dict['7'].push_back('q');
dict['7'].push_back('r');
dict['7'].push_back('s');
dict['8'].push_back('t');
dict['8'].push_back('u');
dict['8'].push_back('v');
dict['9'].push_back('w');
dict['9'].push_back('x');
dict['9'].push_back('y');
dict['9'].push_back('z');
} void dfs(int dep, int maxDep, string &s, string ans)
{
if (dep == maxDep)
{
ret.push_back(ans);
return;
} for(int i = 0; i < dict[s[dep]].size(); i++)
dfs(dep + 1, maxDep, s, ans + dict[s[dep]][i]);
} vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
if(digits.size()==0)
return ret;
createDict();
dfs(0, digits.size(), digits, "");
return ret;
}
};

leetcode 题解 || 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 ...

随机推荐

  1. 读书笔记(javascript语言精粹)

    1. 注释: 在js中,/**/为块注释,//为行注释:但块注释在有些情况下是不安全的.如: /* var rm_a = /a*/.match(s); */ 上面的注释导致了一个语法错误.所以,建议避 ...

  2. 基于SAAJ的客户端

    概述 SAAJ - SOAP with Attachments API for JAVA 结构图如下: 正文 1. 如何获取soap请求的关键参数 关键的参数有四个: xmlns - xml命名空间如 ...

  3. Bzoj4753/洛谷P4432 [JSOI2016]最佳团体(0/1分数规划+树形DP)

    题面 Bzoj 洛谷 题解 这种求比值最大就是\(0/1\)分数规划的一般模型. 这里用二分法来求解最大比值,接着考虑如何\(check\),这里很明显可以想到用树形背包\(check\),但是时间复 ...

  4. Mysql远程连接报错:SQL Error (1130): Host '192.168.6.128' is not allowed to connect to this MySQL server

    通过SQLyog连接linux中的MySQL报错问题:SQL Error (1130): Host '192.168.6.128' is not allowed to connect to this ...

  5. 德州扑克AI

    德州扑克: 1:outs数,就是所听的牌的数量. 例子: 1:听顺子 4567 outs数就是8,能够成顺子的牌为3和8. 5689 outs数就是4,能够成顺子的牌只有7. 2:听同花     35 ...

  6. shell 执行结果赋给变量

    #将pwd的执行结果放到变量value中保存, value=$(pwd) 另一种方法: value=`pwd`

  7. BZOJ 3997 [TJOI2015]组合数学(单调DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题目大意] 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右 ...

  8. 【左偏树+延迟标记+拓扑排序】BZOJ4003-城池攻占

    [题目大意] 有n个城市构成一棵树,除1号城市外每个城市均有防御值h和战斗变化参量a和v. 现在有m个骑士各自来刷副本,每个其实有一个战斗力s和起始位置c.如果一个骑士的战斗力s大于当前城市的防御值h ...

  9. Android Studio自动化快速实现Parcelable接口序列化

    1.在线安装 然后打开File -> Settings -> Pugins -> Browse Repositories 如下,输入android parcelable code g ...

  10. Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分

    D. Professor GukiZ and Two Arrays 题目连接: http://www.codeforces.com/contest/620/problem/D Description ...