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. SharePoint 2013的100个新功能之开发

    一:SharePoint应用 SharePoint 2013引入了云应用模型来供用户创建应用.SharePoint应用是独立的功能整合,扩展了SharePoint网站的功能.应用可以包含SharePo ...

  2. 响应: 500 OOPS: priv_sock_get_int 错误: 读取目录列表失败

    /************************************************************************* * 响应: 500 OOPS: priv_sock ...

  3. 6-19 Count Connected Components(20 分)

    Write a function to count the number of connected components in a given graph. Format of functions: ...

  4. 数据库Job定时任务

    数据库Job再熟悉不过了,因为经常要数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作.但是,Oracle定时器Job时间的处理上,千变万化,今 ...

  5. python api接口认证脚本

    import requests import sys def acces_api_with_cookie(url_login, USERNAME, PASSWORD, url_access):     ...

  6. Linux function: unshare

    When a new process is created with the clone() system call, a set of flags is provided which tells t ...

  7. lnmp -- 解决Warning: scandir() has been disabled for security reasons in…的问题

    原因:LNMP 0.9禁用了部分存在危险的PHP函数 LNMP0.9禁用的PHP函数包括:passthru, exec, system, chroot, scandir, chgrp, chown, ...

  8. 如何重启 Windows 10 子系统(WSL) ubuntu

    如何重启 Windows 10 子系统(WSL) ubuntu WSL 子系统是基于 LxssManager 服务运行的. 只需要将 LxssManager 重启即可. 可以做成一个 bat 文件. ...

  9. Chrome 的应用功能越来越强大

    Chrome 的应用功能越来越强大 升级到 版本 70.0.3538.77 最早的时候是看到 http 显示地址,现在可以在快捷应用中显示扩展了,还可以看到显示的站点. 现在越来越强大了.

  10. thinkphp配置rewrite模式访问时不生效 出现No input file specified解决方法

    使用thinkphp配置rewire模式的路径访问网站时, 直接复制官网的.htaccess文件的代码复制过去 1 2 3 4 5 6 <IfModule mod_rewrite.c>   ...