LeetCode17 Letter Combinations of a Phone Number
题意:
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.(Medium)
分析:
很好的练习搜索的题目,可以用BFS和回溯法(DFS)两种方式来做一下练习。
自己写的时候没太想明白回溯的写法(参数,返回值等等),写的是BFS的解法,DFS参考的讨论区。
代码1:
class Solution {
public:
vector<string> letterCombinations(string digits) {
string digMap[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> v;
if (digits.size() == ) {
return v;
}
queue<string> q;
for (int i = ; i < digMap[digits[] - ''].size(); ++i) {
q.push(string(,digMap[digits[] - ''][i] ));
}
for (int i = ; i < digits.size(); ++i) {
int l = q.size();
for (int j = ; j < l; ++j) {
string temp1 = q.front();
q.pop();
for (int k = ; k < digMap[digits[i] - ''].size(); ++k) {
string temp2 = temp1 + digMap[digits[i] - ''][k];
q.push(temp2);
}
}
}
while (!q.empty()) {
v.push_back(q.front());
q.pop();
}
return v;
}
};
回溯法:
class Solution {
private:
string digMap[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
void dfs(vector<string>& v, string& tempStr, int index, string& digits) {
if (index == digits.size()) {
v.push_back(tempStr);
return;
}
for (int i = ; i < digMap[digits[index] - ''].size(); ++i) {
//tempStr += digMap[digits[index] - '0'][i];
tempStr.push_back(digMap[digits[index] - ''][i]); //string的push_back和pop_back以前没用过
dfs(v,tempStr,index + ,digits);
tempStr.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> result;
if (digits.size() == ) {
return result;
}
string tempStr;
dfs(result,tempStr,,digits);
return result;
}
};
LeetCode17 Letter Combinations of a Phone Number的更多相关文章
- 算法练习--LeetCode--17. Letter Combinations of a Phone Number
Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. 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][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- leetcode-algorithms-17 Letter Combinations of a Phone Number
leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- js运动 摩擦运动
<!DOCTYPE HTML> <HTML> <meta http-equiv="Content-Type" content="text/h ...
- Powershell导入订阅号(以Azure中国版为例)
1. 首先,您需要安装Windows Azure Powershell.下载的链接为:http://go.microsoft.com/?linkid=9811175&clcid=0x409 2 ...
- TreeSet介绍
一.TreeSet原理: 1.TreeSet存储对象的时候, 可以排序, 但是需要指定排序的算法 2.Integer能排序(有默认顺序), String能排序(有默认顺序), 自定义的类存储的时候出现 ...
- Step By Step(Lua字符串库) (转)
1. 基础字符串函数: 字符串库中有一些函数非常简单,如: 1). string.len(s) 返回字符串s的长度: 2). string.rep(s,n) 返回字符串s重复n次的结 ...
- sql操作table
1.增加表字段 alter table tbsptrustquotdoc(表名) add chargeapplystate(字段名) char(1)(类型) default '1'(默认值) 2. ...
- C++11用于元编程的类别属性
[C++11用于元编程的类别属性] 许多算法能作用在不同的数据类别; C++ 模板支持泛型,这使得代码能更紧凑和有用.然而,算法经常会需要目前作用的数据类别的信息.这种信息可以通过类别属性 (type ...
- 关于Unity
14年左右的时候开始学习了Unity,一直没有时间总结一些东西,框架机制啥的都不用说了,网上到处都有,虽然Unity是脚本机制,但是熟悉编程的人只要理解透了拿面向对象的思维编码也完全没有问题,这里重新 ...
- FZU2143Board Game(最小费用流)
题目大意是说有一个B矩阵,现在A是一个空矩阵(每个元素都为0),每次操作可以将A矩阵相邻的两个元素同时+1,但是有个要求就是A矩阵的每个元素都不可以超过K,求 这个的最小值 解题思路是这样的,新建起点 ...
- 自定义滚动控件(Pagecontrol)
// // MyPageCorol.h // lejiahui // // Created by iOS开发 on 16/4/10. // Copyright © 2016年 zhongmingwuy ...
- [Microsoft][ODBC SQL Server Driver][DBNETLIB]SQL Server 不存在或访问被拒绝
一般连接sql数据库,IP_connstr="driver={SQL Server}; server=127.0.0.1;database=数据库名字;uid=sa;pwd=密码" ...