[刷题] 17 Letter Combinations of a Phone Number
要求
- 给定一个仅包含数字
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的更多相关文章
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- 全网最详细的Linux命令系列-touch命令
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 命令格式: touch [选项]... 文件... 命令参数: -a 或--tim ...
- [高精度]P1096 Hanoi 双塔问题
Hanoi 双塔问题 题目描述 给定A.B.C三根足够长的细柱,在A柱上放有2n个中间有孔的圆盘,共有n个不同的尺寸,每个尺寸都有两个相同的圆盘,注意这两个圆盘是不加区分的(下图为n=3的情形). 现 ...
- 201871030118-雷云云 实验三 结对项目—《D{0-1}KP 实例数据集算法实验平台》项目报告
项目 内容 课程班级博客 班级链接 这个作业要求链接 作业链接 我的课程学习目标 (1)体验软件项目开发中的两人合作,练习结对编程(2)掌握Github协作开发程序的操作方法(3)学习遗传算法 这个作 ...
- Anacoda下报错conda command not found 以及TypeError: __new__() got an unexpected keyword argument 'serialized_options'
在anacoda安装完成后,执行conda list命令,显示command not found 解决方法: 对于anaconda 2 , 输入export PATH=~/anaconda2/bin ...
- 恒骊学堂的JAVA初步学习总纲--转载
- 17. Vue2.4+新增属性$listeners
现在我们来讨论一种情况,A组件与C组件怎么通信,我们有多少种解决方案? 我们使用VueX来进行数据管理,但是如果项目中多个组件共享状态比较少,项目比较小,并且全局状态比较少,那使用VueX来实现该功能 ...
- JDBC_08_解决SQL注入问题 (登录和注册)
解决SQL注入问题 只要用户提供的信息不参与sql语句的编译过程,那么尽管用户输入的信息中含有sql关键字那么也不会起作用了 要想使用户提供信息不参与sql语句的编译过程,那么必须使用 java.sq ...
- 开坑:mysql相关问题
一. 先过滤后连表和先连表后在mysql中选择的哪一种? 二. left join 和inner join使用场景有什么区别? 三. 第二个问题的衍生问题:left join中where 条件使用对n ...
- 用Taro写一个微信小程序(一)——开始一个项目
一.Taro简介 1.名字由来 Taro['tɑ:roʊ],泰罗·奥特曼,宇宙警备队总教官,实力最强的奥特曼. 2.taro是什么 Taro 是一个开放式跨端跨框架解决方案,支持使用 React/Vu ...
- MySQL8安装教程及问题解决
目录 1.下载MySQL的zip文件,解压,在根目录(bin所在的目录)下创建my.ini文件 2.管理员模式打开命令提示符(shell或者说小黑窗),按以下命令操作. 3.不过......我这里密码 ...