【一天一道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< ...
随机推荐
- nginx平台初识(二) 浏览器 HTTP 协议缓存机制详解
1.缓存的分类 缓存分为服务端侧(server side,比如 Nginx.Apache)和客户端侧(client side,比如 web browser). 服务端缓存又分为 代理服务器缓存 和 反 ...
- Dynamics CRM2013 Form利用window.location.reload()进行全局刷新带来的问题及解决办法
CRM2013以后,表单的保存后变成了局部刷新而非全局刷新,但很多情况下我们需要刷新整个页面,通过刷新页面来使脚本执行或者业务规则执行来实现某些业务效果,一般我们会使用window.location. ...
- Select标签 根据value值默认选中 Jquery
网上找了很多都是错的,不行的. 下面方法可以的 <script type="text/javascript"> $(document).ready(function() ...
- struts1.x mvc简单例程
因为项目需要,需要使用struts1.x .因此学习下struts框架. 例程从struts 实现MVC框架入手,完成一次request请求. 一.前台 两个jsp文件 index.jsp: 只有一个 ...
- 一步步创建Qt Widget项目+TextFinder案例(摘自笔者2015年将出的《QT5权威指南》,本文为试读篇)
创建一个基于应用的QtWidget应用程序 这个手册描述了怎样使用QtCreater创建个一个小的Qt应用程序,Text Finder.它是Qt工具Text Finder例子的简写版本.这个应用 ...
- Gem/Bundle/Rvm
做过Ruby项目的人可能有过我一样的感受,rubygems.org在中国的访问太慢了,每次我们bundle install都要等老长时间,而我们通过浏览器去下载对应的gems文件时却速度刷刷的... ...
- Android开发学习之路--UI之ListView
这里再学习写android的ListView,其实我们都使用过ListView,就像手机的联系人,就是用的ListView了.下面就实现下简单的ListView吧,首先是xml文件中添加相关的代码: ...
- 谈谈Ext JS的组件——布局的使用方法续一
盒子布局 盒子布局主要作用是以水平(Ext.layout.container.HBox)或垂直方式(Ext.layout.container.VBox)来划分容器区域.这也是比较常有的布局方式. 使用 ...
- UNIX环境高级编程——文件和目录
一.获取文件/目录的属性信息 int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); in ...
- 使用WakeLock使Android应用程序保持后台唤醒
在使用一些产品列如微信.QQ之类的,如果有新消息来时,手机屏幕即使在锁屏状态下也会亮起并提示声音,这时用户就知道有新消息来临了.但是,一般情况下手机锁屏后,Android系统为了省电以及减少CP ...