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.

解题思路:

心想要是我们可以得到这样一棵树,那么我们的问题就变得很简单了,只需要利用DFS遍历一遍问题就解决了。

当然不建立这棵树,也是可以进行DFS遍历的。


  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5. class Solution {
  6. public:
  7. vector<string> letterCombinations(string digits) {
  8. if (digits.size() == 0)
  9. return result;
  10. init();
  11. dfs(0, (int)digits.size(), digits, "");
  12. return result;
  13. }
  14. private:
  15. vector<string> result;
  16. map<char, string> dict;
  17. void init() {
  18. dict['0'] = " ";
  19. dict['1'] = "";
  20. dict['2'] = "abc";
  21. dict['3'] = "def";
  22. dict['4'] = "ghi";
  23. dict['5'] = "jkl";
  24. dict['6'] = "mno";
  25. dict['7'] = "pqrs";
  26. dict['8'] = "tuv";
  27. dict['9'] = "wxyz";
  28. }
  29. void dfs(int dep, int max_dep, string digits, string tmp) {
  30. if (dep == max_dep) {
  31. result.push_back(tmp);
  32. return;
  33. }
  34. //for循环遍历每个数字所对应的所有字符的情况,dfs递归是不断的深入
  35. for (int i = 0; i < dict[digits[dep]].size(); i++) {
  36. dfs(dep + 1, max_dep, digits, tmp + dict[digits[dep]][i]);
  37. }
  38. }
  39. };
  40. int main() {
  41. string input = "23";
  42. Solution* solution = new Solution();
  43. vector<string> result = solution->letterCombinations(input);
  44. for (int i = 0; i < result.size(); i++) {
  45. cout << result[i] << endl;
  46. }
  47. return 0;
  48. }

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]Letter Combinations of a Phone Number题解

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

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

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

  4. [LeetCode] Letter Combinations of a Phone Number

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

  5. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  6. LeetCode Letter Combinations of a Phone Number (DFS)

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

  7. [LeetCode] Letter Combinations of a Phone Number 回溯

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

  8. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  9. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. NSURLSession访问网络数据

    1.NSMutableURLRequest的设置 //创建NSMutableURLRequest对象 NSMutableURLRequest *request = [NSMutableURLReque ...

  2. HTML5 video标签播放视频下载原理

    HTML5 video https://github.com/remy/html5demos/blob/master/demos/video.html <video preload=" ...

  3. word自定义格式 并下载

    /** * * @param pRun * @param 20 间距 * @param fontSize 字体大小 * @param bold 是否加粗 * @param underLine 是否下划 ...

  4. SQL语句处理一些修改、新增、删除、修改属性操作(MySql)

    方法一: 直接(手动)去修改数据库名称,数据库表名称,数据库列名称.列属性 方法二: 使用SQL语句去修改 -- 修改表名 ALTER TABLE tableName RENAME newTableN ...

  5. 约在CBD,吃饭

    午饭当然是外卖. CBD上班的同仁们不用约,都去了一间叫“大食堂”的餐厅. 它在商业街繁华地段的二楼,有1000平米.你不知道么,餐馆们都躲到找不着的角落,变成了厨房,这里的租金便宜得很.但它不做饭, ...

  6. spring cloud 学习研究- spring-cloud-microservice-example

    spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...

  7. 【QUESTION】

    1. HTTP和HTTPS的区别? 2. Soap协议的理解? 3. 一个成功项目,从代码层分析存在可能的问题? 4. mysql 容载技术有哪些? 5. mysql的性能优化有哪些心得? ----- ...

  8. IFRAM随内部长宽高变化

    <iframe src="" id="iframe_CustomerVisitRecord" width="700" height=& ...

  9. 测试 Prism 语法高亮

    测试 Prism 对 C 语言的语法高亮 #include <stdio.h> #include "math.h" int main(void) { long int ...

  10. C#控制台项目更改运行文件

    这个是极光推送的C# demo,里面有几个文件,要先后运行.这是第一次遇见,所以一下子找不到北,摸索了好一会儿才知会.于是做了这面一个记录. 右击鼠标,查看属性弹出这个这面,然后选择要启动的对象.保存 ...