LeetCode OJ-- Letter Combinations of a Phone Number ***
https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/
使用递归,深搜,使用 map 保存已经处理过的结果
class Solution {
public:
unordered_map<string, vector<string> > memory;
vector<string> letterCombinations(string digits) {
vector<string> ans;
if(digits.size() == )
{
ans.push_back("");
return ans;
}
memory.clear();
// initialize
vector<string> piece;
piece.push_back("a");
piece.push_back("b");
piece.push_back("c");
memory.insert(make_pair("",piece));
vector<string> piece3;
piece3.push_back("d");
piece3.push_back("e");
piece3.push_back("f");
memory.insert(make_pair("",piece3));
vector<string> piece4;
piece4.push_back("g");
piece4.push_back("h");
piece4.push_back("i");
memory.insert(make_pair("",piece4));
vector<string> piece5;
piece5.push_back("j");
piece5.push_back("k");
piece5.push_back("l");
memory.insert(make_pair("",piece5));
vector<string> piece6;
piece6.push_back("m");
piece6.push_back("n");
piece6.push_back("o");
memory.insert(make_pair("",piece6));
vector<string> piece7;
piece7.push_back("p");
piece7.push_back("q");
piece7.push_back("r");
piece7.push_back("s");
memory.insert(make_pair("",piece7));
vector<string> piece8;
piece8.push_back("t");
piece8.push_back("u");
piece8.push_back("v");
memory.insert(make_pair("",piece8));
vector<string> piece9;
piece9.push_back("w");
piece9.push_back("x");
piece9.push_back("y");
piece9.push_back("z");
memory.insert(make_pair("",piece9));
subLetter(digits, ans);
return ans;
}
void subLetter(string &digits, vector<string> &ans)
{
// already in
if(memory.find(digits) != memory.end())
{
ans = memory[digits];
return;
}
// split digits
int mid = digits.size()/;
string former = digits.substr(,mid);
string latter = digits.substr(mid,digits.size() - mid);
vector<string> strsFormer;
vector<string> strsLatter;
if(memory.find(former) != memory.end())
strsFormer = memory[former];
else
subLetter(former, strsFormer);
if(memory.find(latter) != memory.end())
strsLatter = memory[latter];
else
subLetter(latter,strsLatter);
for(int i = ; i < strsFormer.size(); i++)
for(int j = ; j < strsLatter.size(); j++)
{
string temp = strsFormer[i] + strsLatter[j];
ans.push_back(temp);
}
memory.insert(make_pair(digits,ans));
return;
}
};
LeetCode OJ-- Letter Combinations of a Phone Number ***的更多相关文章
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 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 ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【leetcode】 Letter Combinations of a Phone Number(middle)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【JAVA、C++】LeetCode 017 Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 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手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
随机推荐
- 爬虫之Scarpy.Request
一 .Request 1.request Scarpy中的HTTP请求对象 1.1.Requse的构造 #我们ctrl+左键可以看到Scarpy.Request的代码 class Request(ob ...
- python正则表达式入门篇
文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...
- python内置函数-排列组合函数
product 笛卡尔积 (有放回抽样排列) permutations 排列 (不放回抽样排列) combinations 组合,没有重复 (不放回抽样组合) combinations_with_re ...
- opencv使用日记之一:平台搭建Mat类以及图像的读取修改
平台搭建就摸了一整天时间,真的是...不说了,最后我选择的是 opencv3.0(2015/06/04) + win7 + vs2012 注意opencv的版本不同导入的库文件是不一样的,所以请 ...
- hive操作语句
设置属性: //设置本地执行作set hive.exec.mode.local.auto=true; //设置动态分区 set hive.exec.dynamic.partition=true; se ...
- WEBs
http://www.cnblogs.com/wupeiqi/articles/5341480.html 请求方式8种: 1.GET:请求指定页面信息,并返回实体 2.POST:向指定资源提交数据进行 ...
- 程序集链接器(AL.exe)
AL.exe使用程序可以生成一个EXE文件或者DLL PE文件(其中只包含对其他模块中的类型进行描述的一个清单). 不要在普通的命令行窗口中编译,请先打开C:\ProgramData\Microsof ...
- Wordpress Uncaught TypeError: b(...).not(...).filter(...).mediaelementplayer is not a function
Wordpress 插件页面报错如下图: 原因及解决方法: 引入了两次 jquery.js 或 jquery.js 定义的变量导致报错,删除在插件页面自己引入的 jquery,js 即可解决报错. 大 ...
- LeetCode668马在棋盘上的概率
已知一个 NxN 的国际象棋棋盘,棋盘的行号和列号都是从 0 开始.即最左上角的格子记为 (0, 0),最右下角的记为 (N-1, N-1). 现有一个 “马”(也译作 “骑士”)位于 (r, c) ...
- [oldboy-django][2深入django]Form总结
1 form总结 # Form数据格式验证 - 原理: - 流程 a.写类LoginForm(Form): 字段名 = fields.xxFields() # 验证规则,本质是正则表达式(fields ...