[LeetCode] Letter Combinations of a Phone Number(bfs)
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)的更多相关文章
- LeetCode:17. Letter Combinations of a Phone Number(Medium)
1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...
- [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 OJ: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
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 (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- 24.Letter Combinations of a Phone Number(电话号对应的字符组合)
Level: Medium 题目描述: Given a string containing digits from 2-9 inclusive, return all possible lette ...
随机推荐
- Mac terminal 解压压缩
tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gunz ...
- 2015ACM/ICPC亚洲区长春站 G hdu 5533 Dancing Stars on Me
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- BZOJ3118 : Orz the MST
对于树边显然只需要减少权值,对于非树边显然只需要增加权值 设i不为树边,j为树边 X[i]:i增加量 X[j]:j减少量 C[i]:修改1单位i的代价 对于每条非树边i(u,v),在树上u到v路径上的 ...
- 百度搜索词&淘宝搜索词 接口实现
百度和淘宝并没有正式的提供一个公开API给我们用,但是经过分析他们的源代码,还是找到了解决方法. 1 2 3 4 5 6 7 8 9 /*baidu&taobao callback*/ fun ...
- NSJSONSerialization介绍
ios5中apple增加了解析JSON的api——NSJSONSerialization.网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON.TouchJSON. ...
- 几种进入mysql的方法
1.首先mysql服务得打开(运行cmd命令也可以net start mysql) 2.运行cmd,打开mysq 3.mysql命令行打开mysql 4.图形管理工具,如phpMyadmin
- IE6 BUG 汇总
1.IE6双倍边距bug 当页面内有多个连续浮动时,如本页的图标列表是采用左浮动,此时设置li的左侧margin值时,在最左侧呈现双倍情况.如外边距设置为10px, 而左侧则呈现出20px,解决它的方 ...
- Gao Big 深圳行
day 1 来到深圳的国际化大都市之后,首先要做的事情就是···坐地铁?? 到了旅店,已经比较晚了,又折腾了两三下.. day 2 第二天一早,一行人出发来到深圳大学(nv shen su she)参 ...
- Nginx 笔记与总结(10)Nginx 与 PHP 整合
Apache + PHP 的编译 和 Nginx + PHP 的编译,区别: Apache 一般把 PHP 当作自己的一个模块来启动: Nginx 则是把 HTTP 请求变量(如 get,user_a ...
- jquery选中将select下拉框中一项后赋值给text文本框
jquery选中将select下拉框中一项后赋值给text文本框,出现无法将第一个下拉框的value赋值给文本框 因为select默认选中第一项..在选择第一项时,便导致无法激发onchange事件. ...