leetcode 题解 || Letter Combinations of a Phone Number 问题
problem:
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.
thinking:
(1)满足要求的数字为2~9。
(2)用一个array保存数字相应的字符,再用dfs枚举全部解
code:
class Solution {
private:
map<char, vector<char> > dict;
vector<string> ret;
public:
void createDict()
{
dict.clear();
dict['2'].push_back('a');
dict['2'].push_back('b');
dict['2'].push_back('c');
dict['3'].push_back('d');
dict['3'].push_back('e');
dict['3'].push_back('f');
dict['4'].push_back('g');
dict['4'].push_back('h');
dict['4'].push_back('i');
dict['5'].push_back('j');
dict['5'].push_back('k');
dict['5'].push_back('l');
dict['6'].push_back('m');
dict['6'].push_back('n');
dict['6'].push_back('o');
dict['7'].push_back('p');
dict['7'].push_back('q');
dict['7'].push_back('r');
dict['7'].push_back('s');
dict['8'].push_back('t');
dict['8'].push_back('u');
dict['8'].push_back('v');
dict['9'].push_back('w');
dict['9'].push_back('x');
dict['9'].push_back('y');
dict['9'].push_back('z');
} void dfs(int dep, int maxDep, string &s, string ans)
{
if (dep == maxDep)
{
ret.push_back(ans);
return;
} for(int i = 0; i < dict[s[dep]].size(); i++)
dfs(dep + 1, maxDep, s, ans + dict[s[dep]][i]);
} vector<string> letterCombinations(string digits) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ret.clear();
if(digits.size()==0)
return ret;
createDict();
dfs(0, digits.size(), digits, "");
return ret;
}
};
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 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 ...
随机推荐
- 转:LLVM与Clang的概述及关系
转:http://www.cnblogs.com/saintlas/p/5738739.html LLVM是构架编译器(compiler)的框架系统,以C++编写而成,用于优化以任意程序语言 ...
- js中__proto__和prototype的区别和关系
首先,要明确几个点:1.在JS里,万物皆对象.方法(Function)是对象,方法的原型(Function.prototype)是对象.因此,它们都会具有对象共有的特点.即:对象具有属性_ ...
- Master定理学习笔记
前言 \(Master\)定理,又称主定理,用于程序的时间复杂度计算,核心思想是分治,近几年\(Noip\)常考时间复杂度的题目,都需要主定理进行运算. 前置 我们常见的程序时间复杂度有: \(O(n ...
- 【BZOJ 1057】 1057: [ZJOI2007]棋盘制作
1057: [ZJOI2007]棋盘制作 Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源 于易经的思想,棋盘是一个8*8大小的 ...
- 【BZOJ 1095】 1095: [ZJOI2007]Hide 捉迷藏 (括号序列+线段树)
1095: [ZJOI2007]Hide 捉迷藏 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉迷藏游戏 ...
- [BZOJ4246]两个人的星座(计算几何)
4246: 两个人的星座 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 101 Solved: 55[Submit][Status][Discuss ...
- bzoj 2803 [Poi2012]Prefixuffix 兼字符串hash入门
打cf的时候遇到的问题,clairs告诉我这是POI2012 的原题..原谅我菜没写过..于是拐过来写这道题并且学了下string hash. 字符串hash基于Rabin-Karp算法,并且对于 ...
- CDOJ 1281 暴兵的卿学姐 构造题
暴兵的卿学姐 题目连接: http://acm.uestc.edu.cn/#/problem/show/1281 Description 沈宝宝又和卿学姐开始玩SC2了! 自从沈宝宝学会新的阵型后,就 ...
- #iOS问题记录# UIWebView滑动到底部
最近看Tmall的iOS APP,在Tmall的商品简介页面,当拖动到最底部时,会提示继续向上拖动,“查看图文详情”: 觉得这个设计挺好的.闲来无事,自己UIWebView模仿一下,问题是检查UIWe ...
- Eclipse、svn插件、subclipse的安装
1.下载subclipse的安装包 URL:http://subclipse.tigris.org/servlets/ProjectDocumentList?folderID=2240 ...