17.Letter Combinations of a Phone Number(Back-Track)
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)的更多相关文章
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- 刷题17. Letter Combinations of a Phone Number
一.题目说明 题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合. 如输入23所有可能的输出: ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
随机推荐
- MYeclipes项目导入导出
导入: 右键,import,
- iOS-----使用AddressBook管理联系人
使用AddressBook管理联系人 iPhone手机通常都是自带的Contacts应用,包括所有联系人的性(last name).名(first name).电话.E-mail地址.住址.生日等各种 ...
- I.MX6 Android mmm convenient to use
# # 主要是记录mmm的简便自动化执行方式,为了减少键盘输入 # ]; then echo echo " Usage: ./remmm.sh <source dirctory> ...
- 十四年风雨路 苹果iMac电脑进化论
1998年起,在CEO乔布斯的带领下,苹果先后创造除了“软糖”iMac G3.“台灯”iMac G4和“像框”G5.iMac凭借其漂亮的外形和强大的性能,迅速赢得了消费者们的喜爱,甚至改变了整个人类社 ...
- VirtualBox的vdi映像导入遇到的uuid冲突问题 (转)
virtualbox导入vdi文件时出现下面的问题: 打开hard disk D:\software\GT5.0.0.vdi 失败 Cannot register the hard disk 'D ...
- linux中bin与sbin目录的作用及区别介绍
linux中bin与sbin目录的作用及区别介绍 本文介绍下,linux中的二个主要目录:bin与sbin,它们的作用与区别,学习linux的朋友可以参考下 在linux系统中,有两个重要的目录:bi ...
- ASI接口
Asynchronous Serial Interface ,异步串行接口,用于传送码流的一个标准DVB接口. 在目前的DVB-C系统设备的传输接口有两种MPEG2视频码流传输接口标准:异步串行接口A ...
- java Long
1. Long.valueOf(b) 返回的是对象 public static Long valueOf(String s) throws NumberFormatException { )); } ...
- spring RestTemplate用法详解
spring RestTemplate用法详解 spring 3.2.3 框架参考有说明 21.9 Accessing RESTful services on the Client
- 设置修改CentOS系统时间和时区
1.yum install ntp,安装时间服务ntpdate time-a.nist.gov && hwclock -w (跟网络同步时间,并且写入主板BIos) 2.chkconf ...