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. 《转》深入理解Activity启动流程(三)–Activity启动的详细流程1

    本文原创作者:Cloud Chou. 出处:本文链接 本系列博客将详细阐述Activity的启动流程,这些博客基于Cm 10.1源码研究. 深入理解Activity启动流程(一)--Activity启 ...

  2. Beta阶段第2周/共2周 Scrum立会报告+燃尽图 11

    作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2411] 版本控制:https://git.coding.net/liuyy08 ...

  3. 作业要求 20181030-1 Alpha发布用户使用报告

    作业链接[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2325] 队名:二次元梦之队 组长:刘莹莹 组员:周昊 潘世维  王玉潘 赵美增 ...

  4. L194

    For many of us, asking for help is it difficult concept. We may feel as if we are admitting a weakne ...

  5. JSONP、CORS解决跨域问题

    一.为什么会有跨域问题? 是因为浏览器的同源策略是对ajax请求进行阻拦了,但是不是所有的请求都给做跨域,对href属性都不拦截. 二.解决跨域问题的两种方式 JSONP CORS 三.JSONP 先 ...

  6. 2017年7月ROS学习资料小结

    <孙子兵法·谋攻篇>:"上兵伐谋,其次伐交,其次伐兵,其下攻城:攻城之法为不得已." 任何发生在自己国土上的战争,即便胜利,也饱含屈辱. ----~~~~----Gaz ...

  7. Locust 关联

    关联的概念 用户登入后,服务器通常会给用户返回一个sessionID, 用户每次登陆,服务器返回的都会不同. 那么在自动化测试中,让系统自动登入账号就会被中断.那么我们可以通过取得服务器每次返回的se ...

  8. ionic2中跨页面回传值

    1.在跳转到新页面时传入一个contactsCallback的参数,在该参数的函数定义中做出一个承诺. 注意:最开始我本来是采用如下图方式的,但是很不幸,出现了问题,问题所在就是关于这个this的作用 ...

  9. Python3.5 源码安装 Ubuntu16.04环境

    安装源码编译所需的各种依赖库:(Ubuntu16.04环境下) sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-de ...

  10. imshow(K)和imshow(K,[]) 的区别

    参考文献 imshow(K)直接显示K:imshow(K,[])显示K,并将K的最大值和最小值分别作为纯白(255)和纯黑(0),中间的K值映射为0到255之间的标准灰度值.