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"]. 思路I:iteration,广度优先。三重for循环,分别循环数字字符串、每个数字对应的字母、已有的result。时间复杂度O(n*3*(3^n))
class Solution {
public:
vector<string> letterCombinations(string digits) {
if(digits=="") return result; m.clear();
m.insert(make_pair('',"abc"));
m.insert(make_pair('',"def"));
m.insert(make_pair('',"ghi"));
m.insert(make_pair('',"jkl"));
m.insert(make_pair('',"mno"));
m.insert(make_pair('',"pqrs"));
m.insert(make_pair('',"tuv"));
m.insert(make_pair('',"wxyz")); result.push_back("");
int resultSize;
for(int i = ; i < digits.length(); i++){//traverse digits
resultSize = result.size();
cout << "size= " << resultSize << endl;
for(int j = ; j < m[digits[i]].length(); j++){ //traverse the character in the digit(except first)
for(int k = ; k < resultSize; k++){ //traverse all current result
result.push_back(result[k] + m[digits[i]][j]);
}
}
for(int k = ; k < resultSize; k++){ //deal with first digit: directly add in the current result
result[k] += m[digits[i]][];
}
} return result;
}
private:
unordered_map<char, string> m;
vector<string> result; };

思路II: recursion,深度优先。将最外层循环转换成递归,递归的depth表示是第几个数字。每次遍历要加上当前数字对应的字母(for循环),加好后递归调用。

class Solution {
public:
vector<string> letterCombinations(string digits) {
m.clear();
m.insert(make_pair('',"abc"));
m.insert(make_pair('',"def"));
m.insert(make_pair('',"ghi"));
m.insert(make_pair('',"jkl"));
m.insert(make_pair('',"mno"));
m.insert(make_pair('',"pqrs"));
m.insert(make_pair('',"tuv"));
m.insert(make_pair('',"wxyz")); int len = digits.length();
dfs("", digits,);
return result;
}
void dfs(string s, string& digits, int depth){
char c = digits[depth]; for(int i = ; i < m[c].length(); i++){
if(depth==digits.length()-) {
result.push_back(s+m[c][i]);
}
else dfs(s+m[c][i], digits, depth+);
}
}
private:
unordered_map<char, string> m; //map是用红黑树实现查找,O(logn); unordered_map是用Hash table实现查找,O(1)
vector<string> result;
};

17.Letter Combinations of a Phone Number(Back-Track)的更多相关文章

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

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

  2. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

  3. 刷题17. Letter Combinations of a Phone Number

    一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...

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

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

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. 17. Letter Combinations of a Phone Number

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

  7. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  9. Leetcode 17.——Letter Combinations of a Phone Number

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

  10. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

随机推荐

  1. 201621123006 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

  2. 记录一些js框架用途

    accounting.min.js 货币格式化alertify.min.js 提示信息库amd.loader.js 按需动态加载js文件angular-cookies.js 处理cookieangul ...

  3. Redis3.0集群

    Redis集群介绍 Redis 集群是一个提供在多个Redis间节点间共享数据的程序集. Redis集群并不支持处理多个keys的命令,因为这需要在不同的节点间移动数据,从而达不到像Redis那样的性 ...

  4. Windows7 SP1 64bit配置IIS7.5和ASP.NET4

    一.安装前的环境 1. Windows7 SP1 64bit: 2. 在安装IIS7.5之前,安装了Visual Studio 2010或.NET Framework4: 二.安装IIS7.5 1.  ...

  5. 《DSP using MATLAB》Problem 2.14

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  6. 转 HTTP.SYS 详解

    http.sys 是一个位于Win2003和WinXP SP2中的操作系统核心组件, 能够让任何应用程序通过它提供的接口,以http协议进行信息通讯. 温馨提示:如果用户不慎删除了该驱动文件,不用担心 ...

  7. Eclipse添加中文javadoc

    SUN官方API中文版[JDK1.6]1.6API文档(中文)的下载地址:ZIP格式用来设置javadoc,下载地址:http://download.java.net/jdk/jdk-api-loca ...

  8. 【转】Ubuntu13.04配置:Vim+Syntastic+Vundle+YouCompleteMe

    原文网址:http://www.cnblogs.com/csuftzzk/p/3435710.html 序言 使用Ubuntu和vim已经有一段时间了,对于Vim下的插件应用,我总是抱着一股狂热的态度 ...

  9. C# List的深复制

    1.关于深拷贝和浅拷贝 C#支持两种类型:值类型和引用类型 值类型(Value Type):如 char, int, float,枚举类型和结构类型 引用类型(Reference Type):如Cla ...

  10. Mysql root 用户密码忘记后重置root密码

    [windows] 1.停止mysql服务:打开命令行窗口CMD,Net stop mysql 2.用另外一种方式启动Mysql:在命令行进入到mysql的安装路径下的bin目录下使用 mysqld- ...