[LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9
inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
- Input: "23"
- Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
这道题让我们求电话号码的字母组合,即数字2到9中每个数字可以代表若干个字母,然后给一串数字,求出所有可能的组合,相类似的题目有 Path Sum II,Subsets II,Permutations,Permutations II,Combinations,Combination Sum 和 Combination Sum II 等等。这里可以用递归 Recursion 来解,需要建立一个字典,用来保存每个数字所代表的字符串,然后还需要一个变量 level,记录当前生成的字符串的字符个数,实现套路和上述那些题十分类似。在递归函数中首先判断 level,如果跟 digits 中数字的个数相等了,将当前的组合加入结果 res 中,然后返回。我们通过 digits 中的数字到 dict 中取出字符串,然后遍历这个取出的字符串,将每个字符都加到当前的组合后面,并调用递归函数即可,参见代码如下:
解法一:
- class Solution {
- public:
- vector<string> letterCombinations(string digits) {
- if (digits.empty()) return {};
- vector<string> res;
- vector<string> dict{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- letterCombinationsDFS(digits, dict, , "", res);
- return res;
- }
- void letterCombinationsDFS(string& digits, vector<string>& dict, int level, string out, vector<string>& res) {
- if (level == digits.size()) {res.push_back(out); return;}
- string str = dict[digits[level] - ''];
- for (int i = ; i < str.size(); ++i) {
- letterCombinationsDFS(digits, dict, level + , out + str[i], res);
- }
- }
- };
这道题也可以用迭代 Iterative 来解,在遍历 digits 中所有的数字时,先建立一个临时的字符串数组t,然后跟上面解法的操作一样,通过数字到 dict 中取出字符串 str,然后遍历取出字符串中的所有字符,再遍历当前结果 res 中的每一个字符串,将字符加到后面,并加入到临时字符串数组t中。取出的字符串 str 遍历完成后,将临时字符串数组赋值给结果 res,具体实现参见代码如下:
解法二:
- class Solution {
- public:
- vector<string> letterCombinations(string digits) {
- if (digits.empty()) return {};
- vector<string> res{""};
- vector<string> dict{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
- for (int i = ; i < digits.size(); ++i) {
- vector<string> t;
- string str = dict[digits[i] - ''];
- for (int j = ; j < str.size(); ++j) {
- for (string s : res) t.push_back(s + str[j]);
- }
- res = t;
- }
- return res;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/17
类似题目:
参考资料:
https://leetcode.com/problems/letter-combinations-of-a-phone-number/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合的更多相关文章
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
- 017 Letter Combinations of a Phone Number 电话号码的字母组合
给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
随机推荐
- SQL Server 随机数,随机区间,随机抽取数据rand(),floor(),ceiling(),round(),newid()函数等
在查询分析器中执行:select rand(),可以看到结果会是类似于这样的随机小数:0.36361513486289558,像这样的小数在实际应用中用得不多,一般要取随机数都会取随机整数.那就看下面 ...
- 在公有云AZURE上部署私有云AZUREPACK以及WEBSITE CLOUD(二)
前言 (二)建立虚拟网络环境,以及域控和DNS服务器 1搭建虚拟网络环境 在Azure上创建虚拟网络.本例选择的是东南亚数据中心.后面在创建虚机的时候,也选择这个数据中心. VNet Name: ...
- [下载]北京新版小学英语五年级上册mp3点读APP
义务教育教科书小学英语五年级上册点读软件.根据2014年北京教改版教材编写,发音标准.实现点读功能.点到哪里读到哪里.哪里不会点哪里!北京教育科学研究院编写,北京出版社出版.ISBN:97872001 ...
- 明显调用的表达式前的括号必须具有(指针)函数类型 编译器错误 C2064
看到“明显调用的表达式前的括号必须具有(指针)函数类型”这句时我才发现我的语文水平有多烂,怎么看都看不懂,折腾了半天才知道是哪里出了问题. 举个简单的例子 class CTest { void (CT ...
- 你必须知道的Microsoft SQL Server一
不知道为什么我Win10环境下安装的Sqlserver2012,智能提示的功能基本上没有用,没办法,我还是选择安装插件SQL Prompt 5吧.下载地址:http://www.uzzf.com/so ...
- [连载]《C#通讯(串口和网络)框架的设计与实现》- 8.总体控制器的设计
目 录 第八章 总体控制器的设计... 2 8.1 总控制器的职能... 2 8.2 组装和释放部件... 3 8.3 ...
- linux使用wkhtmltopdf报错error while loading shared libraries:
官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...
- 【干货分享】前端面试知识点锦集01(HTML篇)——附答案
一.HTML部分 1.浏览器页面有哪三层构成,分别是什么,作用是什么? 构成:结构层.表示层.行为层分别是:HTML.CSS.JavaScript作用:HTML实现页面结构,CSS完成页面的表现与风格 ...
- 用css隐藏元素的5种方法
.green { width: 100px; height: 100px; background-color: #a0ee00; text-align: center; float: left; ma ...
- CentOS7 查看IP、Gateway、DNS、Hostname
1.查看IP# ip addr 2.查看路由# ip route 3.查看DNS# cat /etc/resolv.conf 4.查看主机名# hostname