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遍历一遍问题就解决了。

当然不建立这棵树,也是可以进行DFS遍历的。


#include <iostream>
#include <map>
#include <vector> using namespace std; class Solution {
public:
vector<string> letterCombinations(string digits) {
if (digits.size() == 0)
return result;
init();
dfs(0, (int)digits.size(), digits, "");
return result;
}
private:
vector<string> result;
map<char, string> dict;
void init() {
dict['0'] = " ";
dict['1'] = "";
dict['2'] = "abc";
dict['3'] = "def";
dict['4'] = "ghi";
dict['5'] = "jkl";
dict['6'] = "mno";
dict['7'] = "pqrs";
dict['8'] = "tuv";
dict['9'] = "wxyz"; }
void dfs(int dep, int max_dep, string digits, string tmp) {
if (dep == max_dep) {
result.push_back(tmp);
return;
}
//for循环遍历每个数字所对应的所有字符的情况,dfs递归是不断的深入
for (int i = 0; i < dict[digits[dep]].size(); i++) {
dfs(dep + 1, max_dep, digits, tmp + dict[digits[dep]][i]);
}
}
}; int main() {
string input = "23";
Solution* solution = new Solution();
vector<string> result = solution->letterCombinations(input);
for (int i = 0; i < result.size(); i++) {
cout << result[i] << endl;
}
return 0;
}

LeetCode——Letter Combinations of a Phone Number的更多相关文章

  1. LeetCode: Letter Combinations of a Phone Number 解题报告

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

  2. [LeetCode]Letter Combinations of a Phone Number题解

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

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

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

  4. [LeetCode] Letter Combinations of a Phone Number

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

  5. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  6. LeetCode Letter Combinations of a Phone Number (DFS)

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

  7. [LeetCode] Letter Combinations of a Phone Number 回溯

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

  8. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  9. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. TPS04-J. 使用线程池时确保ThreadLocal变量每次都初始化

    线程池可以提供这种保障,一旦你的代码开始执行了,被分配来执行这个task的线程在执行完你的task之前不会做别的事情. 所以不用担心执行到一半被别的task改了 thread local 的变量. 由 ...

  2. XML和JSON数据格式对比

    概念 XML 扩展标记语言 (Extensible Markup Language, XML) ,用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语 ...

  3. Molile App(HTTP/HTML)—Analyze Traffic

  4. VS快速生成JSON数据格式对应的实体

          有固定好的Json数据格式,你还在手动敲对应的实体吗?有点low了!步入正题,这是一个json字符串,先去验证JSON数据格式(http://www.bejson.com/)如下: { & ...

  5. Unity: Invalid serialized file version xxx Expected version: 5.3.4f1. Actual version: 5.3.5f1.

    Unity发布安卓项目,如果直接使用Unity打包APK一切Ok,导出Google项目 使用Idea打包 一进去直接Crash. 报错: 1978-2010/? E/Unity﹕ Invalid se ...

  6. iOS 单例的销毁

    今天做项目的时候,对于不同的用户,需要创建不同的数据库.但是退出登录切换账号时,因为用单例创建数据,导致切换账号不会切换数据.所以,需要销毁单例.销毁单例时,调用以下的代码: 在创建单例的那个类中,调 ...

  7. Web攻防之XSS,CSRF,SQL注入

    摘要:对Web服务器的攻击也可以说是形形色色.种类繁多,常见的有挂马.SQL注入.缓冲区溢出.嗅探.利用IIS等针对Webserver漏洞进行攻击.本文结合WEB TOP10漏洞中常见的SQL注入,跨 ...

  8. Base64 的那些事儿

    一.Base64是什么? Base64是一种编码的格式.是将信息流(字节流)按照一定的规范,重新组合,显示出完全不相关内容的编码格式. ps.定义是我自己总结的,我觉得对于知识的定义,只要简洁,不错误 ...

  9. Python中的传值和引用

    我写这个主要是给自己看,内容也就是便于自己理解,可能会不正确,但目前来看代码测试的结果是对的. python中一切皆对象. 当我们赋值时: a = 1 其实是先创建了一个整数常量1(也是一个对象,且已 ...

  10. 移动设备如何打开RMS加密的文档

    关键字:RMS. AZure RMS.IPhone.Android.Office365.Sharepoint.Exchange 最近总是碰到要求用苹果手机及安卓手机阅读RMS加密文档的需求,经过查找相 ...