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. HDU1561 The more, The Better(树形DP)

    题目是有n个存有宝藏的城堡,攻克任何一个城堡都需要先攻克0个或其他1个城堡,问攻克m个城堡最多能得到多少宝藏. 题目给的城堡形成一个森林,添加一个超级根把森林连在一起就是树了,那么就考虑用树型DP: ...

  2. BZOJ1769 : [Ceoi2009]tri

    将所有点极角排序,建立线段树,线段树每个节点维护该区间内所有点组成的上下凸壳. 对于一个查询,二分查找出相应区间的左右端点,在线段树上得到$O(\log n)$个节点,在相应凸壳上三分查找出与斜边叉积 ...

  3. BZOJ3640 : JC的小苹果

    设$f[i][j]$表示$hp$为$i$,在$j$点的概率,$d[i]$表示$i$的度数,$w[i]$表示经过$i$点要扣掉的血量. 对于$j$到$k$这条边,$f[i-w[k]][k]+=\frac ...

  4. Bestcoder Round# 80

    [1003 Sequence] 指数循环节,注意a mod p = 0的情况.此时你的循环节如果返回0,这时你会输出1,而实际上应该是0 #include <algorithm> #inc ...

  5. Mac or Centos 下如何编译objective-C

    #import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...

  6. javascript中的function

    function / 对象 所有的变量和方法名的:以字母,$ _开头其他随便,尽量使用英文字母命名,见名知意注意点:不允许使用关键字定义变量和方法的名称====函数即方法,方法即函数====百度:ja ...

  7. 第五次实验报告 java 网络编程

    20145306 第五次 java 实验报告 实验内容 客户端与服务器连接,客户端中输入明文,利用DES算法加密,DES的秘钥用RSA公钥密码中服务器的公钥加密,计算明文的Hash函数值,一起传送给客 ...

  8. iOS segue 跳转

    场景描述: 要实现在tableViewController 的界面A里,点击一个cell ,跳转到第二个viewController的界面B .在第二个界面里做相应操作. 我的做法,利用sb,在A 里 ...

  9. IO的生命周期

    ● 将来自cache的数据封装成bio submit_bh->submit_bh_wbc 此时IO还在fs层 ● 进入block IO层 submit_bh_wbc->submit_io- ...

  10. Memcached 笔记与总结(6)PHP 实现 Memcached 的一致性哈希分布算法

    首先创建一个接口,有 3 个方法: addServer:添加一个服务器到服务器列表中 removeServer:从服务器列表中移除一个服务器 lookup:在当前的服务器列表中找到合适的服务器存放数据 ...