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 ...
随机推荐
- MAC 下编译 ANDROID P 源码 提示 internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12" "10.13"]
MAC 下编译 ANDROID P 源码出现下面的问题: ninja: no work to do. [21/21] out/soong/.bootstrap/bin/soong_build out/ ...
- mysql 多条语句合并查询
select count(*) from matches where StartTime > 1519315200 and endtime < 1519401600 and matchty ...
- c/c++ socket函数详解
c/c++ socket函数详解 注意: 使用socketAPI前,要先将相关链接库(Ws2_32.lib)加入链接,并使用WSAStartUp函数初始化.每个socket函数都可能失败(返回-1), ...
- lmdb数据格式
http://deepdish.io/2015/04/28/creating-lmdb-in-python/ https://lmdb.readthedocs.org/en/release/ http ...
- YAML文件格式入门
YAML快速入门 https://www.jianshu.com/p/97222440cd08 https://yaml.org/spec/1.2/spec.pdf http://nodeca.git ...
- 使用LINQ获取List列表中的某个字段值
使用LINQ获取列表中的某个字段值,下面以获取员工列表中的编号字段为例子. 1.使用Select方法 List<Emplayee> emplayeeList = GetEmplayeeLi ...
- [Luogu4899][IOI2018] werewolf 狼人
luogu sol \(\mbox{IOI2018}\)的出题人有没有看过\(\mbox{NOI2018}\)的题目呀... \(\mbox{Kruskal}\)重构树+二维数点. 题目相当于是问你从 ...
- Python协程 Gevent Eventlet Greenlet
https://zh.wikipedia.org/zh-cn/%E5%8D%8F%E7%A8%8B 协程可以理解为线程中的微线程,通过手动挂起函数的执行状态,在合适的时机再次激活继续运行,而不需要上下 ...
- spring与hibernate注解及XML方式集成
spring与hibernate注解及XML方式集成 Hibernate Xml方式 该种方式需要在sessionFactory中引入对应的hbm.xml文件,样例如下: <!-- spring ...
- String的不变性到final在java中用法
final在Java语言里面啥意思 final修饰一个类,那么这个类就是不可继承.string就是一个非常有名的被final修饰的类,不过他的更加有名的是“不可被修改”. 究竟什么是不可改变?stri ...