[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 ...
随机推荐
- POJ1659 Frogs' Neighborhood(Havel定理)
给一个无向图的度序列判定是否可图化,并求方案: 可图化的判定:d1+d2+……dn=0(mod 2).关于具体图的构造,我们可以简单地把奇数度的点配对,剩下的全部搞成自环. 可简单图化的判定(Have ...
- [转]C++设计模式:Builder模式
Builder模式要解决的问题是,当我们要创建很复杂的对象时,有时候需要将复杂对象的创建过程和这个对象的表示分离开来.由于在每一步的构造过程中可以映入不同参数,所以步骤相同但是最后的对象却不一样.也就 ...
- win8 中实现断点续传
1) Resume method does resume on cases where resume is possible. Meaning if the server accepts range- ...
- CCSpriteBatchNode的优化性能
当将大量精灵加载到CCLayer时,如果直接利用[self addChild:sprite]去加载,每加载一个精灵,都必须open,draw,close, 而利用 CCSpriteBatchNode去 ...
- javascript第一弹——对象
一. 什么是对象 对象是包含一组变量(称为属性)和函数(称为方法)的集合的实例. javascript中所有事物都是对象 javascript有很多内建对象 javascript允许自定义对象 对象只 ...
- JQuery图形插件,Highcharts平滑线条处理方法
第一种:静态数据 $('#THChartDiv').highcharts({ chart: { type: 'spline' }, title: { text:过程线' }, xAxis: { tit ...
- 20145302张薇《Java程序设计》实验三报告
20145302张薇<Java程序设计>实验三:敏捷开发与XP实践 实验内容 使用git上传代码 使用git实现代码开发实践 实现代码的重载 使用git上传代码 git init git ...
- [转] - 经典SQL语句大全
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
- 【iCore2双核心板视频教程三】iM_LAN 100M 以太网模块TCP压力测试(更新视频教程)
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...
- 简单实现Redis缓存中的排序功能
1.在实现缓存排序功能之前,必须先明白这一功能的合理性.不妨思考一下,既然可以在数据库中排序,为什么还要把排序功能放在缓存中实现呢?这里简单总结了两个原因:首先,排序会增加数据库的负载,难以支撑高并发 ...