题目描述

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

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

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

解题思路

典型的利用回溯法解题。首先构造一个map,令每个数字对应各个英文字母,然后对于给出的字符串进行递归映射:

  • 对于每个数字字符,分别添加其对应的字符到结果字符串中,并向后递归
  • 若遍历到了字符串结尾,则说明各数字映射完毕,将当前字符串加入到结果集合中

代码

 class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.empty())
return res;
map<char, string> numToStr;
numToStr['']="abc";
numToStr['']="def";
numToStr['']="ghi";
numToStr['']="jkl";
numToStr['']="mno";
numToStr['']="pqrs";
numToStr['']="tuv";
numToStr['']="wxyz";
find(digits, , "", res, numToStr);
return res;
}
void find(string digits, int idx, string s, vector<string> &res, map<char, string> numToStr){
if(idx == digits.size())
res.push_back(s);
else
for(int i = ; i < numToStr[digits[idx]].size(); i++)
find(digits, idx+, s+numToStr[digits[idx]][i], res, numToStr);
}
};

LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)的更多相关文章

  1. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  2. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

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

  3. 电话号码的字母组合 · Letter Combinations of a Phone Number

    [抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...

  4. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  5. LeetCode第[17]题(Java):Letter Combinations of a Phone Number

    题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...

  6. 【Leetcode】【Medium】Letter Combinations of a Phone Number

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

  7. 【leetcode刷题笔记】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. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

  9. [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###

    描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...

随机推荐

  1. centos配置mutt和msmtp发送邮件

    一.安装mutt yum install mutt -y 二.配置mutt vim /etc/Muttrc 在里面找到下面几行,并将内容修改为你自己的内容(下面几行分布在不同位置,请耐心查找,记得去掉 ...

  2. 25 Python之模块与包

    一.模块   模块就是一个包含了python定义和申明的文件,文件名就是模块的名字加上.py的后缀/ 模块的分类:     1.使用python编写的py文件     2.已被编译位共享库或者DLL或 ...

  3. JS通过sort(),和reverse()正序和倒序

    sort()正序   var array1 = [0,1,5,10,15]; array1.sort();//结果为:0,1,10,15,5   请注意,上面的代码没有按照数值的大小对数字进行排序,要 ...

  4. Nginx配置,请求到tomcat中

    一.web服务器分为两类 1.web服务器 1)Apache服务器 2)Nginx 3)IIS 2.web 应用服务器 1)tomcat 2)resin 3)jetty 区分:web服务器不能解析js ...

  5. Java高并发程序设计学习笔记(二):多线程基础

    转自:https://blog.csdn.net/dataiyangu/article/details/86226835# 什么是线程?线程的基本操作线程的基本操作新建线程调用run的一种方式调用ru ...

  6. redis一键部署脚本

    1.新建一个名为 auto_install_redis.sh的文件 2.将下面脚本拷贝到文件中,具体步骤在注释里面 #环境 linux #一键安装redis,在linux环境中使用脚本运行该文件(sh ...

  7. Ubuntu中用sudo apt-get install makeinfo时,出错:Unable to locate package

    背景: 在准备ARM交叉编译环境时,执行命令: DISTRO=fsl-imx-x11 MACHINE=imx6qsabresd source fsl-setup-release.sh -b build ...

  8. Runtime.getRuntime.exec()执行linux脚本导致程序卡死问题

    rumtime程序执行中出现卡住,执行成果达不到预期的标准.查看输出流以及错误流程是否内存占满了.开两个线程来运行输出流程和错误流程. rumtime运行windows脚本执行是要添加执行环境 cmd ...

  9. python分别获取虚拟网卡和真实网卡ip

    #!/usr/bin/python # -*- coding: utf-8 -*- import commands import socket import fcntl import struct C ...

  10. 牛客练习赛46 C 华华跟奕奕玩游戏 (期望,概率)(详解)

    链接:https://ac.nowcoder.com/acm/contest/894/C 来源:牛客网 华华跟奕奕玩游戏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...