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. IPython notebook快捷键(Jupyter notebook)

    转自“https://blog.csdn.net/eswai/article/details/53642802” 本文整理了神器IPython Notebook(或Jupyter Notebook)的 ...

  2. Request.Form("id")与Request.QueryString("id")的区别

    获取以表单提交的post类型的值,也就是获取<form method="post">这种方式提交的值request包含各种方式的值,没有指名是什麽,所以Request. ...

  3. VS2017 ATL创建ActiveX编程要点

    VS2017 ATL创建ActiveX控件编程要点: 一.创建vs项目需要安装器visual studio installer中: 安装 visual studio扩展开发中的 用于x86和x64的V ...

  4. github 添加完sshkey之后仍然需要输入密码

    1.在家目录下创建.netrc文件,内容如下 machine github.com login username password password window下创建:在用户文件夹如C:\Users ...

  5. MSYS2 使用

    在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...

  6. HDFS的Java API 对文件的操作

    在本次操作中所用到的命令 1.首先启动HDFS $HADOOP_HOME/sbin/start-dfs.sh 2.关防火墙 切换到root用户,执行service iptables stop 3.拷贝 ...

  7. vsftp虚拟用户方式访问

    需求:外部人员需要对公司服务器上某个文件夹内容进行读写操作 文件目录信息:/opt/abc drwxr-xr-x 9 www  www       4096 12月  4 13:02 abc   #注 ...

  8. tcp案例之文件下载器

    文件下载器客户端 import socket def main(): # 1.创建一个tcp socket tcp_client_socket=socket.socket(socket.AF_INET ...

  9. dubbo理解

    Dubbo服务的调用基本上都是出现在分布式项目中,最常见的电商网站.涉及买卖的APP等. 比如某个购物APP,目前最常见的架构就是做成分布式架构,拆分成很多个系统,比如用户模块.短信模块.产品模块.订 ...

  10. 零基础入门学习Python(5)--闲聊之Python的数据类型

    前言 本次主要闲聊一下python的一些数值类型,整型(int),浮点型(float),布尔类型(bool),还有e记法(科学计数法),也是属于浮点型. 数值类型介绍 整型 整型就是我们平时所说的整数 ...