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. 配置nginx作为下载站点

    nginx默认情况是不允许列出整个目录浏览下载 1)autoindex参数详解 autoindex on //on开启目录浏览 autoindex_exact_size off; //off显示出文件 ...

  2. 京东前端:PhantomJS 和NodeJS在网站前端监控平台的最佳实践

    1. 为什么需要一个前端监控系统 通常在一个大型的 Web 项目中有很多监控系统,比如后端的服务 API 监控,接口存活.调用.延迟等监控,这些一般都用来监控后台接口数据层面的信息.而且对于大型网站系 ...

  3. CodeForces 738E Subordinates

    排序,构造. 相当于告诉我们一棵树$n$个节点,每个节点在哪一层,至少需要移动多少个节点,才能让这些节点变成一棵树. 按照层次排个序移动一下就可以了,优先选择那些不是$s$但是层次是$0$的节点,如果 ...

  4. ARM开发板不工作的几个原因

    刚焊了5块ARM(LPC2478)的开发板,上程序测试了一下,发现只有一个板子工作其他四个全部歇菜.努力地找了一会最终发现是板子的来个电阻焊翻了.因为是1206 的封装而且来个电阻在PCB上摆放的位置 ...

  5. css总结——position

    CSS(Cascading Style Sheet),中文翻译为层叠样式表,是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言.在css控制页面中,主要有四种样式:行内样式(style ...

  6. 【20181024T3】小C的宿舍【分治】

    题面 [错解] 好像就是\(|i-j|+|a_i - b_i|\)唉 嗯开始都加i-1,跑一遍,1~(i-1)加1,i~n 减1,线段树维护. 过样例了呢 哎大样例怎么多了那么多啊 跑了个暴力,多得更 ...

  7. [Arc080F]Prime Flip

    [Arc080F]Prime Flip Description 你有无限多的"给给全",编号为1,2,3,....开始时,第x1,x2,...,xN个"给给全" ...

  8. BZOJ 2225 [Spoj 2371]Another Longest Increasing(CDQ分治)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2225 [题目大意] 给定N个数对(xi,yi),求最长上升子序列的长度. 上升序列定义 ...

  9. 【动态规划】【记忆化搜索】hdu5965 扫雷

    f(i,j,k)表示第i行,放的雷的状态为j{0表示不放,1表示往上放,2表示往下放,3表示上下都放},剩余还有k(0<=k<=2)个要放的方案数. 先给出我这个sb写的错误代码,死都没调 ...

  10. JDK源码学习笔记——Integer

    一.类定义 public final class Integer extends Number implements Comparable<Integer> 二.属性 private fi ...