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 ...
随机推荐
- echarts is not defined解决方案
最近在写一个类似于vue的一种cola-ui框架,有兴趣的朋友可以了解一下:http://legacy.cola-ui.com/: 项目中有个业务需要引入Echarts图表的需求,由于是前后端没有分离 ...
- 第十三篇 JS 操作table表格
JS 操作table表格 这节课难度可能高一点,因为没有提前解释if判断.for循环.这节课是直接把这两样用上了,老师先简单介绍一下: if,判断语句,判断就很简单了嘛,假如说1=1(1等于1),当然 ...
- 写两个线程,一个线程打印1-52,另一个线程打印A-Z,打印顺序为12A34B56C......5152Z
题目: 写两个线程,一个线程打印1-52,另一个线程打印A-Z,打印顺序为12A34B56C......5152Z.要求用线程间的通信. /** * 写两个线程,第一个线程打印1-52,第二个线程打印 ...
- java在遍历列表的时候删除列表中某个元素
在遍历list的时候需要删除其中的某些元素,不要用foreach遍历,需要用Iterator. List<String> list = new ArrayList<String> ...
- UltraEdit中的特殊字符
以下特殊字符,可以在替换中用到, ^n--换行 ^t--Tab
- 2019.10.9wechat反弹shell复现
./backdoor.py -f libEGL.dll -s reverse_shell_tcp_inline -P 6666 -H 192.168.106.137 msfconsle 打开msf 在 ...
- 接口开发(login、reg)
接口开发: import flask,json,pymysql,hashlib server = flask.Flask(__name__)# 把当前这个python文件当做一个服务 def my_d ...
- LIS 普及题
题意 给你一个长度为 \(n\) 的序列 \(a\). 问是否存在一个长度为 \(L\) 的上升子序列,即存在 \(\{x_1,x_2,...,x_L\}(x_1\lt x_2\lt ...\lt x ...
- 同步windows时间到linux服务器
输入date -R 查看系统时间 输入命令 ntpdate time.windows.com 同步windows时间到linux
- 集合(一)Collection、List、ArrayList和Vector
一.Collection 集合存放在java.util包中,可以看作是集成好的数据结构,供你调用,十分方便,集合经常拿来和数组对比,其实我觉得没啥可比性,不过还是简单来看看它们的区别: 1.数组长度固 ...