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. asp.net中导出Excel的方法

    一.asp.net中导出Excel的方法: 本文转载 在asp.net中导出Excel有两种方法,一种是将导出的文件存放在服务器某个文件夹下面,然后将文件地址输出在浏览器上:一种是将文件直接将文件输出 ...

  2. Unity Built-in Shader详解三

    上次讲的是Transparent Shader Family,他们是绘制半透明的对象使用的,但他们并不能满足我们全部的要求. Transparent Cutout Shader Family是对半透明 ...

  3. BZOJ4389 : ZYB and Trees

    Link-Cut Tree维护. 每个点x维护以下信息: v:这个点的点权 s:实链上的信息和 st:子树信息和(不包括链上) sa:子树+链上的信息和 as:所有虚儿子的sa的和 则有 s[x]=v ...

  4. BZOJ2690 : 字符串游戏

    离线算法: 先将所有涉及到的串建成字典树,然后用线段树维护dfs序,时间复杂度$O(m\log L)$. 在线算法: 用替罪羊树动态维护Trie树的dfs序即可,时间复杂度$O(L\log L)$. ...

  5. BZOJ2725 : [Violet 6]故乡的梦

    如果S==T,那么答案为0. 如果S与T不连通,那么答案为inf. 否则,S到T的最短路径上至少有一条边. 求出以S为源点的最短路图,是个DAG,随便抓一条S到T的最短路,记为P. 设dpS[x]表示 ...

  6. BZOJ3483 : SGU505 Prefixes and suffixes(询问在线版)

    将每个串正着插入Trie A中,倒着插入Trie B中. 并求出每个串在A,B中的dfs序. 每次查询等价于查询在A中dfs序在[la,ra]之间,在B中dfs序在[lb,rb]之间的串的个数,用主席 ...

  7. 详细讲解css单位px,em和rem的含义以及它们之间的区别

    一.首先介绍一下px px就是css中最基本的长度单位了,用px做单位基本上没什么问题,可以做到让页面按套路精确的展现! 可但是!但可是!如果全篇用px布局会暗藏一个蛋疼的问题,就是当用户和Ctrl滚 ...

  8. 移动WEBAPP开发常规CSS样式总结

    我所使用到的HTML页面标签: Section,div,artical,p,ol,ul,li,header,footer,span,form,input,label,h1,h2,h3 :详细说明我就不 ...

  9. SecureCrt自动化

    Crt自动化 测试 SecureCrt脚本 JS脚本   引言 软件介绍 脚本介绍 引言 在嵌入式公司中,面对大量的网络设备,不论开发同事进行设备开发.测试同事进行大量测试工作还是运维人员进行大量设备 ...

  10. JQuery图形插件,Highcharts平滑线条处理方法

    第一种:静态数据 $('#THChartDiv').highcharts({ chart: { type: 'spline' }, title: { text:过程线' }, xAxis: { tit ...