17. Letter Combinations of a Phone Number[M]电话号码的字母组合
题目
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.

思路
思路一:递归法
首先建立电话按钮上数字与字母的对应关系,很容易就想到了利用C++里map
思路二:回溯法
Tips
递归法
回溯法
C++
- 思路一
map<int,string> table={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};
vector<string> letterCombinations(string digits) {
vector<string> result;
if(digits.length()==0)
return result;
combineNum(result,0,"",digits);
return result;
}
void combineNum(vector<string>& r,int count,const string& a,const string& b){
if (count == b.size()){
r.push_back(a);
return;
}
string curStr = table[b[count] - '0'];
for (char s : curStr){
combineNum(r, count + 1, a + s, b);
}
}
- 思路二
python
17. Letter Combinations of a Phone Number[M]电话号码的字母组合的更多相关文章
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
随机推荐
- python课程设计笔记(二)破冰基本语法
python两种编程方式:交互式与文件式 交互式:语法练习,输一条运行一条 文件式:通用,执行一组语句 注释 #单行注释 ...XXXXX...多行注释 逻辑 没有大括号,按缩进确定逻辑——缩进格数 ...
- Charles设置抓取https请求
1.在手机上设置HTTP代理,将手机与电脑连接. 打开手机设置-WLAN,设置代理 2.(iPhone)使用iPhone自带的浏览器打开Safari,并输入地址:http://charlesproxy ...
- JavaScript 创建对象之单例、工厂、构造函数模式
01单例模式 首先看一个问题,我们要在程序中描述两个人,这两个人都有姓名和年龄,可能刚刚开始学习js的时候会写成这样: var name1 = 'iceman'; var age1 = 25; var ...
- codeforces 468B two set(并查集)
链接 B. Two Sets 题意 给两个集合A B,两个数a b,n个数x,分配n个数到两个集合,要求x , a-x在同一个集合,x , b-x在同一个集合,属于A集合的数输出0,B的输出1,无解输 ...
- css3小叮当(转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 路飞学城Python-Day53
01-jquery的介绍 JS在做项目或者是实现功能的时候,用JS去操作DOM元素非常复杂,代码量大,重复性代码也多 多个元素使用for循环遍历也是非常麻烦的,对于JS使用来说加大了难度 jQuery ...
- SQL SEVER 递归查询
with ts as ( --首先要查询出最原始父级的信息 union all --全连接 select a.fitemclassid,a.fitemid, a.fnumber,a.Fparentid ...
- 解决远程登录mysql命令行无法登录问题
由于甲骨文公司收购了mysql,mysql有商业化的趋势,所以想尝试安装下mariadb . yum install mariadb-server mariadb systemctl start ma ...
- 漫谈 Google 的 Native Client(NaCl) 技术(二)---- 技术篇(兼谈 LLVM)
转自:http://hzx5.blog.163.com/blog/static/40744388201172531637729/ 漫谈 Google 的 Native Client(NaCl) 技术( ...
- [1] first day
一.几个工具包 [1]pandas(数据分析工具) https://zhuanlan.zhihu.com/p/33230331 https://zhuanlan.zhihu.com/p/2501351 ...