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.

用queue实现bfs:

class Solution {
public:
map<char,string> m;
vector<string> letterCombinations(string digits) {
vector<string> result;
m['']="abc";
m['']="def";
m['']="ghi";
m['']="jkl";
m['']="mno";
m['']="pqrs";
m['']="tuv";
m['']="wxyz";
queue<string> q;
string s;
q.push(s);
result = bfs(q,digits);
return result;
}
vector<string> bfs(queue<string> q,string &digits){
vector<string> result; int len = digits.size();
if(len==){
string s;
result.push_back(s);
return result;
} while(!q.empty()){
string s = q.front();
int n = s.size();
q.pop();
if(n==len){
result.push_back(s);
continue;
} string s0(s);
char c = digits[n];
int alphaNum = m[c].size();
for(int i =;i<alphaNum;i++){
s.push_back(m[c][i]);
q.push(s);
s = s0;
}
} return result;
}
};

[LeetCode] Letter Combinations of a Phone Number(bfs)的更多相关文章

  1. LeetCode:17. Letter Combinations of a Phone Number(Medium)

    1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...

  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 解题报告

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

  4. LeetCode OJ: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 电话号码的字母组合

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

  6. [LeetCode] Letter Combinations of a Phone Number

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

  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 (DFS)

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

  9. 24.Letter Combinations of a Phone Number(电话号对应的字符组合)

    Level:   Medium 题目描述: Given a string containing digits from 2-9 inclusive, return all possible lette ...

随机推荐

  1. WPF之TextBox

    1. TextBox实现文字垂直居中 TextBox纵向长度比较长但文字字体比较小的时候,在输入时就会发现文字不是垂直居中的. 而使用中我们发现,TextBox虽然可以设置文字的水平对齐方式,但却没有 ...

  2. HDU 2255 & KM模板

    题意: 一张完备二分图求最优完备匹配. SOL: 这题就不讲什么sol了...毕竟是裸的KM,不会的话可以看老人家的大白鼠,一些问题看代码注释.讲讲经历(悲惨的经历) 刚打完,自信地交上去发现MLE. ...

  3. 使用CSS 3创建不规则图形 文字围绕

    前言 CSS 创建复杂图形的技术即将会被广泛支持,并且应用到实际项目中.本篇文章的目的是为大家开启它的冰山一角.我希望这篇文章能让你对不规则图形有一个初步的了解. 现在,我们已经可以使用CSS 3 常 ...

  4. Java/Js下使用正则表达式匹配嵌套Html标签

    转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...

  5. 冒泡排序:一百以内十个随机数放入数组排序并打印<

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. hdu-acm steps Common Subsequence

    /*这道题是很明显的dp题,状态方程有点不大好想,也许是我刚刚接触dp的缘故吧.dp[i][j]表示字符串s1取前i个字符s2取前j个字符时最大公共子序列的大小,这样的如果s1[i]==s2[j],d ...

  7. codeacademy

    codeacademy http://link.zhihu.com/?target=https%3A//www.youtube.com/playlist%3Flist%3DPLP6DbQBkn9ymG ...

  8. ORACLE SEQUENCE用法

    引用自: http://www.cnblogs.com/hyzhou/archive/2012/04/12/2444158.html 在oracle中sequence就是序号,每次取的时候它会自动增加 ...

  9. Symantec更新服务器

    HTTP liveupdate.symantec.com liveupdate.symantecliveupdate.com   FTP update.symantec.com/opt/content ...

  10. 【iCore3双核心板】扩展引脚分布

    PDF 版下载: http://files.cnblogs.com/files/xiaomagee/iCore3%E6%89%A9%E5%B1%95%E5%BC%95%E8%84%9A%E5%88%8 ...