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"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

又加深了对DFS的认识,在递归函数中,需要把握几个点,总能各个击破。

1.就是变化的量要作为参数来传递,这样也就是每个函数在自己的栈中都有该局部变量,这样就可以在回溯到某个点的时候,他的局部变量不会消失。

2.一般的终止条件中,计算结果

代码:

private:
map<char, vector<char> > dict;
vector<string> ret;
int len;
public:
void createDict()
{
dict.clear();
dict[''].push_back('a');
dict[''].push_back('b');
dict[''].push_back('c');
dict[''].push_back('d');
dict[''].push_back('e');
dict[''].push_back('f');
dict[''].push_back('g');
dict[''].push_back('h');
dict[''].push_back('i');
dict[''].push_back('j');
dict[''].push_back('k');
dict[''].push_back('l');
dict[''].push_back('m');
dict[''].push_back('n');
dict[''].push_back('o');
dict[''].push_back('p');
dict[''].push_back('q');
dict[''].push_back('r');
dict[''].push_back('s');
dict[''].push_back('t');
dict[''].push_back('u');
dict[''].push_back('v');
dict[''].push_back('w');
dict[''].push_back('x');
dict[''].push_back('y');
dict[''].push_back('z');
}
void dfs(int loc,string digits,string temp)
{
if(loc==len){
ret.push_back(temp);
//temp.erase(temp.size()-1);在这里擦除是没有用的,这已经是下一个递归函数,回溯到上一个的时候,它的局部变量temp还是三个字母
return;
}
for (int i=;i<dict[digits[loc]].size();++i)
{
temp.push_back(dict[digits[loc]][i]);
dfs(loc+,digits,temp);
temp.erase(temp.size()-);
}
}
vector<string> letterCombinations(string digits) {
createDict();
vector<string> tempres;
tempres.push_back("");
if(digits.empty()) return tempres;
len=digits.size();
dfs(,digits,"");
return ret;
}
};
int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
Solution so;
so.letterCombinations("");
return ;
}

Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)的更多相关文章

  1. 24.Letter Combinations of a Phone Number(电话号对应的字符组合)

    Level:   Medium 题目描述: Given a string containing digits from 2-9 inclusive, return all possible lette ...

  2. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  4. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  5. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  6. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  7. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

  8. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

随机推荐

  1. JS中的逻辑运算符&&、||,位运算符|,&

    1.JS中的||符号: 运算方法: 只要“||”前面为false,不管“||”后面是true还是false,都返回“||”后面的值. 只要“||”前面为true,不管“||”后面是true还是fals ...

  2. 微信“摇一摇&#183;周边”正式开放

    今日,在微信公开课第三季长沙站现场,微信团队宣布“摇一摇·周边”(以下简称“摇周边”)正式对外开放.拥有微信认证的公众帐号商户,均可通过摇周边的商户申请平台(https://zb.weixin.qq. ...

  3. 分享div、text、Box Shadow(阴影)演示及代码的页面

    附图: 直接上链接:www.css88.com/tool/css3Preview/Box-Shadow.html

  4. iOS Programming View and View Hierarchy 视图和视图等级

    iOS Programming  View and View Hierarchy 视图和视图等级 1.1(1)File → New → Project.. From the iOS section, ...

  5. iOS---数据离线缓存

    离线缓存 为了用户的体验,不需要每次打开App都加载新数据,或者重新请求数据,因此需要把每次浏览的数据保存起来,当下次打开软件时,首先从沙盒中加载数据:或者当软件未联网时,也只能从沙盒中加载旧数据. ...

  6. ECharts Map 属性详解

    $(function() { // 路径配置 require.config({ paths : { // echarts: 'http://echarts.baidu.com/build/dist' ...

  7. SCCM大致安装过程,参考前辈教程完成部署

    本安装sccm主站点服务器准备 参考:http://stephen1991.blog.51cto.com/8959108/1529864 1.  准备三台服务器 ,注:所有服务器需要安装 .net3. ...

  8. cce - 控制台中文环境

    语法 (SYNTAX) cce [-e program] 描述 (DESCRIPTION) 该程序是一个类似于 WZCE , yact 和 chdrv 的控制台中文平台.进入该环境后可以用“空格 + ...

  9. 安装exe4j出现jre不匹配问题

    在安装exe4j 客户端,提示如下错误: 提示的错误信息大意如下:install4j安装时,在本系统中没有找到JRE(JavaRuntime Environment)(版本要求:最低1.5,最高1.6 ...

  10. python 深复制和浅复制

    https://www.python-course.eu/python3_deep_copy.php-------------------------------------------------- ...