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 ...
随机推荐
- Android:控件WebView显示网页 -摘自网络
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- jszs 快速排序
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- T-SQL运算符
运算符 1.算术运算符 算术运算符 说明 + 加法 - 减法 * 乘法 / 除法 % 取模,两个整数相除后的余数 2.位运算符 位运算符 说明 &(与.and) 按位逻辑与运算 |(或.OR) ...
- C++11能用智能指针
[C++11能用智能指针] shared_ptr 是一引用计数 (reference-counted) 指针,其行为与一般 C++ 指针即为相似.在 TR1 的实现中,缺少了一些一般指针所拥有的特色, ...
- Java模板引擎 FreeMarker
@(编程) [TOC] 1. 简介 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写.它是为Java程序员提供的一个开发包.它不是面向最终用户的,而是为程序员 ...
- oracle存储过程返回数据集结果
MSSQL的存储过程返回数据集好简单,直接SELECT 就可以. ORACLE的存储过程返回数据集必须通过游标. 创建ORACLE存储过程 create or replace procedure cx ...
- CodeForces 534B Covered Path (水题)
题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...
- 监控SQL
http://www.cnblogs.com/downmoon/archive/2009/08/12/1544764.html
- UI:字典的两种取值的区别
字典的两种取值的区别 (objectForKey: 和 valueForKey )参考 一般来说 key 可以是任意字符串组合,如果 key 不是以 @ 符号开头,这时候 valueForKey: 等 ...
- C:常量、变量 、 表达式 、运算符、 枚举
常量 变量 表达式 运算符 枚举 1.布尔类型只有真和假 2运算符 >,<,<=,>=,==,!=.判断两个数是否相等要使用双等号‘==’.逻辑运算符的表达式结果非真即假,&a ...