要求

  • 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合
  • 1 不对应任何字母

  

示例

  • 输入:“23”
  • 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

思路

  • 递归向后一位一位生成所有可能字符串
  • s(digits)是digits所能生成的字符串
  • letter(digits[0])是按键digits[0]对应的字符

实现

  • res作为类的私有成员变量
  • 调试代码:30,36,45,49
 1 #include <vector>
2 #include <iostream>
3 #include <string>
4 #include <assert.h>
5
6 using namespace std;
7
8 class Solution {
9
10 private:
11 const string letterMap[10] = {
12 " ",
13 "",
14 "abc",
15 "def",
16 "ghi",
17 "jkl",
18 "mno",
19 "pqrs",
20 "tuv",
21 "wxyz"
22 };
23
24 vector<string> res;
25 // 处理第index位数字
26 // s保存digits[0...index-1]生成的字符串
27 // 找到和digits[index]匹配的字母,获得digits[0...index]生成的解
28 void findCombination(const string &digits, int index, const string &s){
29
30 cout<<index<<" : "<<s<<endl;
31
32 // 终止条件
33 if( index == digits.size() ){
34 // s是一个解,保存
35 res.push_back(s);
36 cout<<"get "<<s<<" , return"<<endl;
37 return;
38 }
39
40 char c = digits[index];
41 assert( c >= '0' && c <= '9' && c != '1' );
42 string letters = letterMap[c-'0'];
43
44 for( int i = 0 ; i < letters.size() ; i ++ ){
45 cout<<"digits["<<index<<"] = "<<c<<" , use "<<letters[i]<<endl;
46 // 处理第index+1位数字
47 findCombination(digits, index + 1, s + letters[i] );
48 }
49 cout<<"digits["<<index<<"] = "<<c<<" complete, return"<<endl;
50 return;
51 }
52 public:
53 vector<string> letterCombinations(string digits) {
54
55 // 初始化
56 res.clear();
57 // 边界情况
58 if( digits == "" )
59 return res;
60
61 findCombination(digits, 0, "");
62
63 return res;
64 }
65 };
66
67 int main(){
68
69 vector<string> res = Solution().letterCombinations("23");
70 for( int i = 0 ; i < res.size() ; i ++ )
71 cout<<res[i]<<endl;
72
73 return 0;
74 }

  

总结

  • 本质是回溯(递归调用到底后,返回上一层,继续调用,直到根节点的所有可能性调用完成)
  • 回溯是一种算法思想,可通过递归实现
  • 和多重循环的区别在于,要处理的字符长度未知
  • 动态规划的本质是在回溯的基础上进行改进,提高效率
  • 复杂度:3^n(指数级O(2^n),n个字母,每个数字3个字母)
  • 暴力枚举解法,效率低,家用计算机n<20

相关

  • 93 Restore IP Addresses
  • 131 Palindrome Partitioning

参考

递归与回溯有什么区别?怎么区分?

https://coding.imooc.com/learn/questiondetail/19706.html

[刷题] 17 Letter Combinations of a Phone Number的更多相关文章

  1. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

  2. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  3. 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 ...

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. 17. Letter Combinations of a Phone Number

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

  7. Leetcode 17.——Letter Combinations of a Phone Number

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

  8. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

  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. 痞子衡嵌入式:MCUXpresso IDE下在线调试时使用不同复位策略的现象总结

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家分享的是MCUXpresso IDE下在线调试时使用不同复位策略的现象总结. 本篇实际上是<IAR在线调试时设不同复位类型可能会导致i.M ...

  2. 【Prolog - 1.0 基础语法与概念】

    [概述] Prolog的语法与其它常用语言(如C,JAVA等)不同,它更接近于自然语言. [实例] 当我想表示"Mia是以女人"这个事实(之后会提到事实这个概念)的时候,我可以这么 ...

  3. 软工2021个人阅读作业#2——构建之法和CI/CD的运用

    项目 内容 这个作业属于哪个课程 2021学年春季软件工程(罗杰 任健) 这个作业的要求在哪里 2021年软工-热身阅读作业#2 我在这个课程的目标是 了解和掌握现代软件开发和项目管理技术,锻炼在大规 ...

  4. 宝塔linux7.4.2/windows6.8 的版本中的安全随笔

    在2020/8.23宝塔官方发布了一条关于宝塔linux7.4.2和Windows6.8版本中存在的重大的安全隐患 通知来源https://www.bt.cn/bbs/thread-54644-1-1 ...

  5. 这一次,彻底搞懂 Go Cond

    hi,大家好,我是 haohongfan. 本篇文章会从源码角度去深入剖析下 sync.Cond.Go 日常开发中 sync.Cond 可能是我们用的较少的控制并发的手段,因为大部分场景下都被 Cha ...

  6. C语言-字符串函数的实现(二)之strcpy

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  7. aws eks ebs StorageClass PersistentVolume PersistentVolumeClaim

    aws EBS 提供存储资源 Amazon EBS CSI 驱动程序的安装,请参考https://docs.aws.amazon.com/zh_cn/eks/latest/userguide/ebs- ...

  8. 【故障公告】数据库服务器再次 CPU 100% 引发全站故障

    今天五一劳动节的一大早 5:50-6:30 期间,我们使用的阿里云 RDS SQL Server 数据库实例再次出现 CPU 100% 问题,引发全站故障,由此给您带来麻烦,请您谅解. 我们发现故障后 ...

  9. 1016 Phone Bills

    A long-distance telephone company charges its customers by the following rules: Making a long-distan ...

  10. hdu4846 最大子正方形(dp)

    题意:       给你一个图,让你找到最大的子矩形. 思路:       之前做过一个最大子矩阵,记得当时是用三种方法做的,两种都是瓶颈法,第三种是dp,结果今天的用瓶颈吧怎么都过不去,哎!不知道为 ...