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 ...
随机推荐
- luogu P4365 [九省联考2018]秘密袭击coat
luogu 这里不妨考虑每个点的贡献,即求出每个点在多少个联通块中为第\(k\)大的(这里权值相同的可以按任意顺序排大小),然后答案为所有点权值\(*\)上面求的东西之和 把比这个点大的点看成\(1\ ...
- java实现spark常用算子之count
import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.a ...
- Object.keys()返回对象自身可枚举属性组成的数组
Object.keys()方法是对一个对象的key遍历,会把key组成一个数组返回 示例: // 参数为数组时,返回的是数组的索引 let arr1 = [1, 2, '3'] console.log ...
- 87. Scramble String (Java)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...
- Spark写入HBase(Bulk方式)
在使用Spark时经常需要把数据落入HBase中,如果使用普通的Java API,写入会速度很慢.还好Spark提供了Bulk写入方式的接口.那么Bulk写入与普通写入相比有什么优势呢? BulkLo ...
- Jansson库的使用简介
一.Jansson的安装: 二.jansson相关的API: https://jansson.readthedocs.io/en/latest/apiref.html#c.json_t string ...
- Ansible批量远程管理Windows主机(部署与配置)
一.测试环境介绍 Ansible管理主机: 系统: CentOS6.8 IP Addr: 172.16.10.22 Linux管理服务器需安装pip.pywinrm插件 Windows客户端主机: ...
- npm上发布vue插件
1.初始化项目 vue init webpack-simple XXXXX(此处为插件名) 使用vue创建一个简单的项目,删除src中除了main.js和app.vue外的文件,清空app.vue中无 ...
- 第五章 动画 48:动画-使用transition-group元素实例列表动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Spring相关概念
DIP: Dependency Inversion Principle.翻译过来是依赖反转原则,也叫依赖倒置原则. 依赖倒置原则是设计模式几个重要原则之一.具体定义就是,底层模块依赖高层模块定义的接口 ...