[刷题] 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 ...
随机推荐
- 开源一周岁,MindSpore新特性巨量来袭
摘要:MindSpore很多新特性与大家见面了,无论是在效率提升.易用性,还是创新方面,都是干货满满. 最近,AI计算框架是业界的热点,各大厂商纷纷投身AI框架的自研发,究其原因:AI框架在整个人工智 ...
- c++ 数组定义及初始化
C ++提供了一种数据结构,即数组,该数组存储一个固定大小的由相同类型元素构成的顺序集合. 数组中的元素存储在一个连续内存位置中,元素可通过数组索引访问, 最低地址对应于第一个元素,最高地址对应于最后 ...
- YARP实现Dapr服务调用的反向代理
楔子 公司即将新开项目,打算用点时髦的技术,需要探探路.之前没做过微服务项目,没有技术栈方面的积(负)累(债), 干脆就上微软的分布式运行时Dapr......嗯......用来服务发现,然后等测试用 ...
- 解决WebStorm无法正确识别Vue3组合式API的问题
1 问题描述 Vue3的组合式API无法在WebStorm中正确识别,表现为defineComponent等无法被识别: 2 尝试方案 猜想这种问题的原因是无法正确识别对应的Vue3库,笔者相信Web ...
- Azure data studio 跨平台数据库管理工具试用
最近折腾 azure sql database 的时候发现了微软的一款新的数据库管理工具: azure data studio.从名字上看 azure data studio 好像是专门为 azure ...
- JAVAEE_Servlet_11_GetAndPost
Get请求和Post请求 * Get请求 和 Post请求各方面分析 - 什么情况下浏览器发送的是Get请求? 1. 通过浏览器的地址栏输入地址,所访问的URL都是get请求,如果以post定义,那么 ...
- 在Bootstrap开发框架基础上增加WebApi+Vue&Element的前端
基于Metronic的Bootstrap开发框架是我们稍早一点的框架产品,界面部分采用较新的Bootstrap技术,框架后台数据库支持Oracle.SqlServer.MySql.PostgreSQL ...
- SpringBoot自定义配置以及IDEA配置提示
本篇文章将会讲解在springboot项目中如何实现自定义配置以及在IDEA或者Eclipse中实现配置项提示,就像spring的配置提示一样 想要做到这点其实非常简单 1.添加依赖 <depe ...
- 服务器安装node全教程
我的服务器centos,安装node时出了点小麻烦,在这里记述我的方法. 1.进入node下载网站https://nodejs.org/en/download/,这里右键复制下载链接 2.进入cent ...
- hdu4494
题意: 给你一些任务,每个任务有自己的开始时间和需要多久能干完,还有就是每个任务都需要一些人,这些人有最多五个种类,各种类之间的人不能相互替换,但是某些工人干完这个活后如果可以在另一个任务 ...