LeetCode OJ: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"].
给出相应的按键,然后要求给出所有可能的字母组合,典型的dfs问题,但是这里的重点在于字典的店里,以及怎么来进行组合,代码如下,原理比较简单,这里就不再赘述了。PS:这题一开始其实没有想明白怎么建立字典,但是实际上通过一个map简单的就可以建立数字与字母之间的关联,代码如下所示:
class Solution {
public:
vector<string> letterCombinations(string digits) {
if(!digits.size())
return ret;
createDic(dic);
dfs(, digits.size(), digits, "");
return ret; } void createDic(map<char, vector<char>> & dic)
{
dic[''].push_back('a');
dic[''].push_back('b');
dic[''].push_back('c');
dic[''].push_back('d');
dic[''].push_back('e');
dic[''].push_back('f');
dic[''].push_back('g');
dic[''].push_back('h');
dic[''].push_back('i');
dic[''].push_back('j');
dic[''].push_back('k');
dic[''].push_back('l');
dic[''].push_back('m');
dic[''].push_back('n');
dic[''].push_back('o');
dic[''].push_back('p');
dic[''].push_back('q');
dic[''].push_back('r');
dic[''].push_back('s');
dic[''].push_back('t');
dic[''].push_back('u');
dic[''].push_back('v');
dic[''].push_back('w');
dic[''].push_back('x');
dic[''].push_back('y');
dic[''].push_back('z');
} void dfs(int dep, int maxDep, string & s, string ans)
{
if(dep == maxDep){
ret.push_back(ans);
return;
} for(int i = ; i < dic[s[dep]].size(); ++i) //这里的dep应该得到注意
dfs(dep+, maxDep, s, ans + dic[s[dep]][i]) ;
} private:
map<char, vector<char>> dic;
vector<string> ret;
};
LeetCode OJ: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 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- linux上mysql访问:Access denied for user 'agtipay'@'iZm5ebiyb4f90ga9xiycgsZ' (using password: YES)
公司的聚合支付测试环境出了一个问题(agtipay用户访问数据的时候出现如题错误),快搞死我两天时间(原谅技术不才),如题.首先明确一下问题: 1.访问拒绝,说明数据库连接这里有问题,数据库连接访问拒 ...
- 【英语学习】How do I stop overthinking at night?
2017-04-03 If you were to say to the grown-ups: "I saw a beautiful house made of rosy brick, wi ...
- Linux Shell脚本编程--字符串截取
Linux 的字符串截取很有用.有八种方法. 假设有变量 var=http://www.aaa.com/123.htm. 1. # 号截取,删除左边字符,保留右边字符. echo ${var#*//} ...
- 20145328 《Java程序设计》第7周学习总结
20145328 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 Lambda 12.1 认识Lambda语法 Lambda 教材的引入循序渐近.深入浅出 Lambda去重 ...
- Java Swing简单的加法器
package test; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class FrameDe ...
- 彻底搞懂hashCode与equals的作用与区别及应当注意的细节
以前写程序一直没有注意hashCode的作用,一般都是覆盖了equals,缺没有覆盖hashCode,现在发现这是埋下了很多潜在的Bug!今天就来说一说hashCode和equals的作用. 先来试想 ...
- QT控件学习
一.QPushButton 1.设置背景色: ui->pushButton->setStyleSheet("background-color: rgb(170, 0, 255)& ...
- 【前端】javaScript 常用技巧总结
javaScript 常用技巧总结 1. 彻底屏蔽鼠标右键 oncontextmenu="window.event.returnValue=false" <table b ...
- 详解WIFI破解-Kali篇
转自: http://www.secbox.cn/hacker/wireless/4877.html 工具: 1:笔记本 2:USB无线网卡(必备) 3:kali系统 4:靠谱字典 第一种方法: 暴力 ...
- Oh My Zsh 插件篇 - 实用工具
Oh My Zsh 除了为我们提供快捷的命令行操作之外,还提供了强大丰富的插件机制,每个社区贡献者都可以贡献自己的插件,让整个生态体系更加丰富完善.今天给大家介绍了一下它的实用工具类插件. 前面我们分 ...