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. OpenStack 认证服务 KeyStone 服务注册(六)

    一)检查keystone是否安装配置成功 1.1删除环境变量的配置 unset OS_AUTH_URL redhat 1.2 请求令牌认证 admin用户,请求认证令牌 openstack --os- ...

  2. spring.net 在demo中的分析

    1.认识spring.net Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入.面向方面编程(AOP).数据访问抽象及AS ...

  3. Struts2 + MySQL 实现分页

    代码结构: package com.action; import java.util.List; import java.util.Map; import com.bean.Pager; import ...

  4. 7/31 CSU-ACM2018暑期训练7-贪心

    比赛链接 A-CSU - 1588 现在有n堆果子,第i堆有ai个果子.现在要把这些果子合并成一堆,每次合并的代价是两堆果子的总果子数.求合并所有果子的最小代价. Input 第一行包含一个整数T(T ...

  5. 洛谷P1730最小密度路径

    题目传送门; 首先理解题目,究其本质就是一个最短路问题,而且数据范围贼水,用floyd完全没问题,但是题目有变化,要求出路径边权值与边数之比,这里就可以考虑在把floyd中的二维数组变为三维,f[ i ...

  6. Eclipse 主题(修改背景色)

  7. Noip2015提高组解题报告

    Day1 T1神奇的幻方 一道简单异常的小模拟,我们只需要确定数字1的位置,然后根据题意枚举即可,简简单单就A了,什么也不卡. 然而这题,我刚开始学OI的时候,因为当时比较蠢,被这题花式吊打啊.... ...

  8. bzoj 2137: submultiple

                                                     Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 23 ...

  9. Objective-c nil, Nil, NULL和NSNull的区别

    在OC中可能经常会遇到 nil,Nil,NULL和NSNull,下面分析一下之间的区别: Symbol Value Meaning NULL (void *)0 literal null value ...

  10. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...