【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode
(一)题目
Given a digit string, 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.
Input:Digit string “23” Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”,
“cd”, “ce”, “cf”].
(二)解题
这题采用回溯法。
以23为例,2对应“abc” ,3对应“def。
第一步:a,b,c
第二部:ad,ae,af,bd,be,bf,cd ,ce,cf
回溯法的意思是,依次递归到最后,如果到最后了就回溯,继续递归。
算法的过程:
首先依次递归得到ad,到digits的长度了,回溯得到a
这下又可以递归了,得到ae,又到digits的长度了,再回溯到a
然后继续循环得到af,f到头了,a也到头了,轮到b上场了!
…….依次下去就把所有的情况都输出了!
class Solution {
public:
string phoneStr[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
string tmp;
vector<string> result;
vector<string> letterCombinations(string digits) {
if(digits.length()==0) return result;
dfs(digits , 0);
return result;
}
void dfs(string &str , int idx)
{
if(idx == str.length())
{
result.push_back(tmp);//递归结束条件
return;
}
else
{
for(int i = 0 ; i < phoneStr[str[idx]-'0'].length() ; i++)
{
tmp = tmp.substr(0,idx);//回溯!
tmp+=(phoneStr[str[idx]-'0'])[i];
dfs(str,idx+1);
}
}
}
};
【一天一道LeetCode】#17. Letter Combinations of a Phone Number的更多相关文章
- 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 ...
- [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
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——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-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. 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-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/ 二.题目大意: 给定一段数字字符串,其中每个数 ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
随机推荐
- UE4使用第三方库读写xml文件
原文链接:http://gad.qq.com/article/detail/7181031 本文首发腾讯GAD开发者平台,未经允许,不得转载 在游戏开发过程中,读写xml几乎已经成为不可或缺的功能,但 ...
- Dynamics CRM2013 从外部系统取到CRM系统的用户头像
CRM从2013开始引入了entityimage的概念,具体这个字段怎么设置的,图像是怎么上传的这里就不谈了.说实在的这玩意在项目中没啥用,所以也没去关注,直到最近遇到了个难题,要在外部系统去获取这个 ...
- SceneKit:简单的3D游戏场景搭建
SceneKit是Apple用来开发休闲3D游戏的框架,不同于底层的OpenGL库,你仅仅需要很少的代码就可以快速看到实际的3D场景效果.下面简单的聊聊搭建一个3D游戏场景需要做的事情. 首先你必须用 ...
- [struts2学习笔记] 第五节 编写struts2的action代码
本文地址:http://blog.csdn.net/sushengmiyan/article/details/40479299 官方文档: http://struts.apache.org/relea ...
- 学习TensorFlow,保存学习到的网络结构参数并调用
在深度学习中,不管使用那种学习框架,我们会遇到一个很重要的问题,那就是在训练完之后,如何存储学习到的深度网络的参数?在测试时,如何调用这些网络参数?针对这两个问题,本篇博文主要探索TensorFlow ...
- ORA-12519: TNS:no appropriate service handler found 解决
select count(*) from v$process --当前的连接数select value from v$parameter where name = 'processes' --数据库允 ...
- 饮一盏Bug留香,唱一曲项目飞扬
沿途的风景 牵挂的项目 两情迢迢 学生档案管理项目在2月的末尾从稍带寒意的季节里完成了第一次迭代,验收的结果不尽善尽美,演示的功能也惨不忍睹,各种"关爱"的点评充斥耳旁 ...
- JAVA之旅(二十七)——字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律
JAVA之旅(二十七)--字节流的缓冲区,拷贝mp3,自定义字节流缓冲区,读取键盘录入,转换流InputStreamReader,写入转换流,流操作的规律 我们继续来聊聊I/O 一.字节流的缓冲区 这 ...
- Android性能优化之Bitmap的内存优化
1.BitmapFactory解析Bitmap的原理 BitmapFactory提供的解析Bitmap的静态工厂方法有以下五种: Bitmap decodeFile(...) Bitmap decod ...
- Inventory Transactions Manager
Overview Inventory Transaction Manager用于处理库存接口表(MTL_TRANSACTION_INTERFACE或者MTL_MATERIAL_TRANSACTIONS ...