https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/

使用递归,深搜,使用 map 保存已经处理过的结果

  1. class Solution {
  2. public:
  3. unordered_map<string, vector<string> > memory;
  4.  
  5. vector<string> letterCombinations(string digits) {
  6. vector<string> ans;
  7.  
  8. if(digits.size() == )
  9. {
  10. ans.push_back("");
  11. return ans;
  12. }
  13.  
  14. memory.clear();
  15.  
  16. // initialize
  17. vector<string> piece;
  18. piece.push_back("a");
  19. piece.push_back("b");
  20. piece.push_back("c");
  21. memory.insert(make_pair("",piece));
  22.  
  23. vector<string> piece3;
  24. piece3.push_back("d");
  25. piece3.push_back("e");
  26. piece3.push_back("f");
  27. memory.insert(make_pair("",piece3));
  28.  
  29. vector<string> piece4;
  30. piece4.push_back("g");
  31. piece4.push_back("h");
  32. piece4.push_back("i");
  33. memory.insert(make_pair("",piece4));
  34.  
  35. vector<string> piece5;
  36. piece5.push_back("j");
  37. piece5.push_back("k");
  38. piece5.push_back("l");
  39. memory.insert(make_pair("",piece5));
  40.  
  41. vector<string> piece6;
  42. piece6.push_back("m");
  43. piece6.push_back("n");
  44. piece6.push_back("o");
  45. memory.insert(make_pair("",piece6));
  46.  
  47. vector<string> piece7;
  48. piece7.push_back("p");
  49. piece7.push_back("q");
  50. piece7.push_back("r");
  51. piece7.push_back("s");
  52. memory.insert(make_pair("",piece7));
  53.  
  54. vector<string> piece8;
  55. piece8.push_back("t");
  56. piece8.push_back("u");
  57. piece8.push_back("v");
  58. memory.insert(make_pair("",piece8));
  59.  
  60. vector<string> piece9;
  61. piece9.push_back("w");
  62. piece9.push_back("x");
  63. piece9.push_back("y");
  64. piece9.push_back("z");
  65. memory.insert(make_pair("",piece9));
  66.  
  67. subLetter(digits, ans);
  68. return ans;
  69. }
  70.  
  71. void subLetter(string &digits, vector<string> &ans)
  72. {
  73. // already in
  74. if(memory.find(digits) != memory.end())
  75. {
  76. ans = memory[digits];
  77. return;
  78. }
  79.  
  80. // split digits
  81. int mid = digits.size()/;
  82. string former = digits.substr(,mid);
  83. string latter = digits.substr(mid,digits.size() - mid);
  84.  
  85. vector<string> strsFormer;
  86. vector<string> strsLatter;
  87. if(memory.find(former) != memory.end())
  88. strsFormer = memory[former];
  89. else
  90. subLetter(former, strsFormer);
  91.  
  92. if(memory.find(latter) != memory.end())
  93. strsLatter = memory[latter];
  94. else
  95. subLetter(latter,strsLatter);
  96.  
  97. for(int i = ; i < strsFormer.size(); i++)
  98. for(int j = ; j < strsLatter.size(); j++)
  99. {
  100. string temp = strsFormer[i] + strsLatter[j];
  101. ans.push_back(temp);
  102. }
  103.  
  104. memory.insert(make_pair(digits,ans));
  105. return;
  106. }
  107.  
  108. };

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. 51nod 1105 二分答案法标准题目

    二分答案法例题,用于练习二分答案的基本思想非常合适,包括了思维方式转换的内容(以前我们所做的一直是利用二分法求得数组元素对应指针之类,但是现在是直接对答案进行枚举). 思路是:首先对输入数组进行排序, ...

  2. A JavaScript Image Gallery

    childNodes property:  The childNodes property is a way of getting information about the children of ...

  3. CQRS之旅——旅程5(准备发布V1版本)

    旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...

  4. OpenCV学习笔记(二) cv::Mat

    部分内容转自:OpenCV Tuturial,ggicci 在OpenCV Tuturial中可查看Mat的初始化与打印方法. Mat本质上是由两个数据部分组成的类: 矩阵头(包含矩阵尺寸,存储方法, ...

  5. 网络流24题:P2762 太空飞行计划问题

    P2762 太空飞行计划问题 题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,E ...

  6. Git的安装及常用操作

    一.Git的安装 1.下载Git,官网地址为:https://git-scm.com/downloads.     2.下载完成之后,双击目录进行安装 3.选择安装目录 4.选择组件,默认即可 5.设 ...

  7. leetcode 【 Remove Element 】python 实现

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  8. plsql编程

    ORACLE PL/SQL编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设计语言,该 ...

  9. 替换掉 in的like操作

    select * from t_unit where '410300060025,410300004005,410300998851,' like '%'+ltrim(rtrim(unitcode)) ...

  10. redis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots

    最近在学习Redis ,在写test测试的时候碰到这个报错: redis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is c ...