[刷题] 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 ...
随机推荐
- 利用别名切换索引流程Elasticsearch 7.7
背景 公司里面现在有es集群,由于时间过长,es集群中的某个索引过大但是未删除,一直在写入的情况下,昨天写入突然停止了,发现是索引超时的问题,这时想到通过创建一个新的索引来进行索引切换 操作 es 集 ...
- 一次死锁导致CPU异常飘高的整个故障排查过程
目录 一.问题详情 top 命令截图 联系腾讯云排查 检查系统日志发现异常 二. 问题解析 三.问题原因 最终结论 四.扩展 进程的几种状态 马后炮 如何快速清理僵尸进程(Z) 内核参数相关 如何查看 ...
- 幻读:听说有人认为我是被MVCC干掉的
@ 目录 前言 系列文章 一.我是谁? 二.为什么有人会认为我是被MVCC干掉的 三.我真的是被MVCC解决的? 四.再聊当前读.快照读 当前读 快照读 五.告诉你们吧!当前读的情况下我是被next- ...
- VUE+Element 前端应用开发框架功能介绍
前面介绍了很多ABP系列的文章<ABP框架使用>,一步一步的把我们日常开发中涉及到的Web API服务构建.登录日志和操作审计日志.字典管理模块.省份城市的信息维护.权限管理模块中的组织机 ...
- Java JFR 民间指南 - 事件详解 - jdk.ObjectAllocationOutsideTLAB
重新申请 TLAB 分配对象事件:jdk.ObjectAllocationOutsideTLAB 引入版本:Java 11 相关 ISSUES: JFR: RecordingStream leaks ...
- 03- HTML基本结构
初始HTML HTML(英文Hyper Text Markup Language的缩写)中文译为"超文本标签语言",主要是通过HTML标签对网页中的文本.图片.声音等内容进行描述. ...
- 关于Snowflake 生成53位ID
1, bug现象: 没有经过处理的Snowflake 生成的是64位bit的唯一ID,但由于多数时候我们前台用到js,但是js只支持53位bit的数值.这样就导致了传到前台的64位的丢失精度. 解决思 ...
- 【CompletableFuture】CompletableFuture测试runAsync()方法调用
问题 CompletableFuture.runAsync() 返回 CompletableFuture<Void>对象,调用CompletableFuture.allOf(f1,f2). ...
- Android最新敲诈者病毒分析及解锁(11月版)
一.样本信息 文件名称:久秒名片赞,(无需积分s)(2)(1)(1).apk 文件大小:1497829字节 文件类型:application/jar 病毒类型:Android.CtLocker 样本包 ...
- Netcat瑞士军刀的简单使用
目录 Netcat 常用参数: 常见的用法: 端口扫描: 聊天 文件传输 反弹shell 蜜罐 Netcat Netcat 常称为 nc,拥有"瑞士军刀"的美誉.nc 小巧强悍,可 ...