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. xmind8 Mac破解版(思维导图) 附序列号

    链接: https://pan.baidu.com/s/1PNdLRGpz_jhfPmWAIbLRfw 提取码: ruvm 复制这段内容后打开百度网盘手机App,操作更方便哦 小伙伴们XMind 8 ...

  2. git 学习笔记1

    目前我属于粗放型的[学习者],接下来需要做一些改变,让自己更加规范.首先需要学习的就是版本控制系统,本科在工作室的时候使用过一点Subversion,不过到现在已经基本没有印象了.git现在越来越成为 ...

  3. 如何参与一个GitHub开源项目?

    如何参与一个GitHub开源项目? 摘要:本文是Github官如何参与一个GitHub开源项目方给出的参与Github上开源项目的一些指导,对希望加入开源社区的开发者是一个不错的参考. 最近一年开源项 ...

  4. JS性能分析(测试代码运行时间)

    //性能优化 console.time("timer"); for(var i=0;i<10000;i++){} console.timeEnd("timer&qu ...

  5. pdf 使用模板下载

    //根据模板下载模板 /** * * 政策5-8条的创建的pdf的模板 */public String createPdfCashTemplate(PdfCashParam pdfCashParam) ...

  6. 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory

    rm -f 删除目录文件夹时出现:rm: cannot remove `/data/wwwroot/backidc': Is a directory cannot remove is a direct ...

  7. fast rcnn,faster rcnn使用cudann加速问题

    之前在fast rcnn,faster rcnn编译过程中USE_CUDNN := 1这一项一直是注释掉的(即不使用cudnn加速),编译会报错: 之所以会这样,是因为fast rcnn,faster ...

  8. vc++实现控制USB设备启用与否

    #include <WINDOWS.H>      #include <TCHAR.H>      #include <SETUPAPI.H>      //#in ...

  9. console.log()与console.dir()

    console.log()可以取代alert()或document.write(),在网页脚本中使用console.log()时,会在浏览器控制台打印出信息. console.dir()可以显示一个对 ...

  10. 第3节 mapreduce高级:4、倒排索引的建立

    倒排索引建立 需求分析 需求:有大量的文本(文档.网页),需要建立搜索索引 最终实现的结果就是哪个单词在哪个文章当中出现了多少次 思路分析: 首选将文档的内容全部读取出来,加上文档的名字作为key,文 ...