LeetCode——Letter Combinations of a Phone Number
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的更多相关文章
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode] Letter Combinations of a Phone Number 回溯
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
- leetcode Letter Combinations of a Phone Number python
class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...
随机推荐
- NSURLSession访问网络数据
1.NSMutableURLRequest的设置 //创建NSMutableURLRequest对象 NSMutableURLRequest *request = [NSMutableURLReque ...
- HTML5 video标签播放视频下载原理
HTML5 video https://github.com/remy/html5demos/blob/master/demos/video.html <video preload=" ...
- word自定义格式 并下载
/** * * @param pRun * @param 20 间距 * @param fontSize 字体大小 * @param bold 是否加粗 * @param underLine 是否下划 ...
- SQL语句处理一些修改、新增、删除、修改属性操作(MySql)
方法一: 直接(手动)去修改数据库名称,数据库表名称,数据库列名称.列属性 方法二: 使用SQL语句去修改 -- 修改表名 ALTER TABLE tableName RENAME newTableN ...
- 约在CBD,吃饭
午饭当然是外卖. CBD上班的同仁们不用约,都去了一间叫“大食堂”的餐厅. 它在商业街繁华地段的二楼,有1000平米.你不知道么,餐馆们都躲到找不着的角落,变成了厨房,这里的租金便宜得很.但它不做饭, ...
- spring cloud 学习研究- spring-cloud-microservice-example
spring cloud + docker 微服务架构 http://www.open-open.com/lib/view/open1437363835818.html 实例项目 https://gi ...
- 【QUESTION】
1. HTTP和HTTPS的区别? 2. Soap协议的理解? 3. 一个成功项目,从代码层分析存在可能的问题? 4. mysql 容载技术有哪些? 5. mysql的性能优化有哪些心得? ----- ...
- IFRAM随内部长宽高变化
<iframe src="" id="iframe_CustomerVisitRecord" width="700" height=& ...
- 测试 Prism 语法高亮
测试 Prism 对 C 语言的语法高亮 #include <stdio.h> #include "math.h" int main(void) { long int ...
- C#控制台项目更改运行文件
这个是极光推送的C# demo,里面有几个文件,要先后运行.这是第一次遇见,所以一下子找不到北,摸索了好一会儿才知会.于是做了这面一个记录. 右击鼠标,查看属性弹出这个这面,然后选择要启动的对象.保存 ...