LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)
题目描述
给定一个仅包含数字 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)的更多相关文章
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 电话号码的字母组合 · Letter Combinations of a Phone Number
[抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...
- Java实现 LeetCode 17 电话号码的字母组合
17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...
- LeetCode第[17]题(Java):Letter Combinations of a Phone Number
题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...
- 【Leetcode】【Medium】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【leetcode刷题笔记】Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] 17. 电话号码的字母组合(回溯)
题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...
- [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###
描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...
随机推荐
- LintCode 68---Binary Tree Postorder Traversal
/** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, ri ...
- Hyperledger Fabric 环境搭建(1)
1,Fabric的程序模块组成 Fabric不是一个单独的程序而是由一组模块组成,这些模块中的每一个都是一个可独立运行的可执行文件. (1)peer 主节点模块,负责存储区块链数据,运行维护链码: ( ...
- 第十篇.1、python并发编程之多进程理论部分
一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...
- linux内核驱动module_init解析(1)
本文转载自博客http://blog.csdn.net/richard_liujh/article/details/45669207 写过linux驱动的程序猿都知道module_init() 这个函 ...
- WebSocket是什么原理,为什么可以实现持久连接
本文摘抄自知乎,原文标题:WebSocket 是什么原理?为什么可以实现持久连接? Websocket只是协议而已. 一.WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化, ...
- Spring源代码下载和导入eclipse
下载源代码包并解压,我下载的是3.2版本,下载地址在https://github.com/spring-projects/spring-framework/tree/3.2.x 因为Spring是使用 ...
- HTTP与TCP的区别和联系(转)
https://www.cnblogs.com/baizhanshi/p/8482612.html
- 多线程(二)Object类方法、线程的操作sleep(),join(),interrupt(),yield()
四.Object类简介 Object类是所有类的超类,之所以放在线程部分是因为其方法很多是和线程有关的.比如以下三个: wait()方法.wait(long timeout)和wait(long ti ...
- 模拟客户端向服务器发起请求(从Fiddler抓包到Jmeter接口测试)
一.安装Fiddler 二.配置 在菜单栏Tools->Fiddler Options->Connections,勾选Allow remote computers to connect,默 ...
- 如何制作chrome浏览器插件之一
方法如下: 1.创建一个单独的文件夹,比如说为百度贴吧开发一个插件,就叫TiebaAddion.之后在这个文件夹里创建一个名字为"manifest.json"的文件,在里面写上如下 ...