题意:

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的更多相关文章

  1. 算法练习--LeetCode--17. Letter Combinations of a Phone Number

    Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...

  2. Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  3. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. 69. Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  5. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  6. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  7. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  8. 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 ...

  9. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. HDU ACM 1325 / POJ 1308 Is It A Tree?

    Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. 企业部署Linux应用将拥有更低的整体拥有成本

    企业部署Linux应用将拥有更低的整体拥有成本     使用Linux能为企业的IT解决方案降低TCO(整体拥有成本Total Cost of The Ownership)吗?在面临这个问题时,很多企 ...

  3. Python和Django的Third Libraby分类汇总

    这些第三方包与Python和Django一起构成了强大的生态系统,自己在开发时大大减小工作难度和工作量, 这些包基本上能满足我们的大部分需求.人与人的差距,其中一点是你知道的比他多,这样你就能大大提高 ...

  4. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  5. easyui 表单和自定义验证扩展和js自定义返回值

    ================jsp==========================<form  method="post" id="regfrminp&qu ...

  6. mongodb的查询方式与sql语句对比

    下面是sql和Mongodb对应的一些语法: SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b ...

  7. Json.Net学习笔记

    http://www.cnblogs.com/xiaojinhe2/archive/2011/10/28/2227789.html Newtonsoft.Json(Json.Net)学习笔记 http ...

  8. iPhone中国移动收不到彩信,联通不用设置都可以,具体设置方法:

    打开“设置”. 打开“通用”. 打开“蜂窝移动网络”. 打开“蜂窝数据移动网络”. 在“蜂窝移动数据”一栏中的“APN”处填入“cmnet”. 在“彩信”一栏中的“APN”处填入“cmnet”,“MM ...

  9. Rstudio安装

    1.https://www.r-project.org/下载R语言(注意32位还是46位系统). 2.安装R,尽量默认安装路径,安装路径不要有中文. 3.https://www.rstudio.com ...

  10. 定制个性化的FlashPaper生成的文件

    1:找到已安装FlashPaper目录下的子目录Interface下的文件DefaultViewer2.swf,在此swf文件的基础上实现自己的修改. 2:利用swf反编译工具,这里推荐 硕思闪客精灵 ...