#leetcode刷题之路17-电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。
给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。
示例:
输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。
先看下图解释:

这道题肯定用的是递归。
首先我们可以看到,输入了几个数字,最终答案里的每一个小项的长度就是几。(例如:我们输入了2 3,这是两个数字,那么答案中的ad、ae、等都是由两个字符组成)
接着,我们那图中列举的第一种情况来分析,首先在第一层我们取首数字的第一个字母元素a,第一层就是在遍历首数字对应的字母,在第二层我们遍历第二个数字对应的字母。要注意这里遍历结束的条件:第一层结束的条件是首数字对应的字母遍历完了,第二层结束条件是第二个字母遍历完了,而当已经生成了由两个字母组成的字符串时,就把这个串加入最终的结果队列。
由此看见,输入的数字的个数、遍历到数字对应的第几个字母、当前生成的字符串(长度),这些都是影响递归的因素。同时,变量中还应有存放答案项的vector以及存放数字字母对应关系的map。
#include <iostream>
#include <map>
#include <vector>
using namespace std; void combine(string digits,int seq,string current,vector<string> &ans,map<char ,string>num_letters)
{
if(digits.size()==seq) ans.push_back(current);
else
{
for(int i=;i<num_letters[digits[seq]].size();i++)//每一层的遍历
{
combine(digits,seq+,current+num_letters[digits[seq]][i],ans,num_letters);
}
}
}
vector<string> letterCombinations(string digits) {
vector<string> ans;
map<char,string> num_letters;
if(digits=="") return ans;
num_letters['']="abc";
num_letters['']="def";
num_letters['']="ghi";
num_letters['']="jkl";
num_letters['']="mno";
num_letters['']="pqrs";
num_letters['']="tuv";
num_letters['']="wxyz";
combine(digits,,"",ans,num_letters);
return ans;
} int main() {
string digits="";
vector<string> ans=letterCombinations(digits);
cout<<ans.size()<<endl;
return ;
}
#leetcode刷题之路17-电话号码的字母组合的更多相关文章
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- python -- leetcode 刷题之路
第一题 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], tar ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(三)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(二)
BasedLeetCode LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 ...
- 使用Java+Kotlin双语言的LeetCode刷题之路(一)
LeetCode learning records based on Java,Kotlin,Python...Github 地址 序号对应 LeetCode 中题目序号 1 两数之和 给定一个整数数 ...
- #leetcode刷题之路40-组合总和 II
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合.candidates 中的每个数字在每个组合中只能使用一次.说 ...
- #leetcode刷题之路16-最接近的三数之和
给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存在唯一答案. 例如,给定数 ...
- #leetcode刷题之路13-罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...
- #leetcode刷题之路6- Z 字形变换
将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列.比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下:L C I ...
- leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
随机推荐
- MySQL数据库(6)----配置文件 my.cnf 的使用
1. 使用源码安装好MySQL后,其配置文件一般位于 /usr/local/my.cnf,可以使用如下命令查看查看配置文件的搜索顺序: root@javis:~$ mysqld --help --ve ...
- Android 图文混排 通过webview实现并实现点击图片
在一个开源项目看到是用的webview 实现的 1. 这是在asset中的一个模板html <html> <head> <title>News Detail< ...
- PRML读书笔记——线性回归模型(上)
本章开始学习第一个有监督学习模型--线性回归模型."线性"在这里的含义仅限定了模型必须是参数的线性函数.而正如我们接下来要看到的,线性回归模型可以是输入变量\(x\)的非线性函数. ...
- 检查 NaN 数据值 (C/C++/Python 实现)
NaN 是 Not a Number 的缩写.它是一个数值类型值,通常在浮点计算中,表示未定义或无法表示的值.而且,不能直接使用相等运算符 (==) 检查 NaN.由于在程序中,nan == nan ...
- Sentinel配置及部署
一.sentinel.conf port 26379 dir /opt/redis-3.0.7/dataSentinel sentinel monitor mymaster 192.168.1.15 ...
- VS2013个版本密钥(亲测可用)
Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visual Studio Premium 2013 KEY(密钥) ...
- find bugs设置
- January 07 2017 Week 1st Saturday
Procrastination is the thief of time. 拖延乃是光阴之窃贼. My parents always tell me that things ought to be d ...
- java 方法修改主函数里基本数据类型和引用数据类型的区别
public class Dog { public void Age(int age) {//副本新建的age age++;//对副本修改 System.out.println(age); } pub ...
- IOS XMPP(即时通讯的框架)
#import "AppDelegate.h" #import "XMPPFramework.h" /* * 在AppDelegate实现登录 1. 初始化XM ...