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 ...
随机推荐
- shell脚本中的交互式输入自动化
shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了. 1 利用重定向 重定向的方法应该是最简单的 例: 以下的te ...
- 动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击
题目: 1010: 魔兽争霸之最后的反击 Time Li ...
- 3、python中的字符串
一.前言 字符串是python中重要的数据类型.字符串就是一段文本,在python中用引号来标示. 二.字符串分类 字符串根据使用场景不同,一共分成3类: (1)单引号.双引号创建的单行字符串: 在单 ...
- 6 json和ajax传递api数据
1 2 3 4 https://swapi.co/ <h1>Hello Reqwest!</h1> <script> var a = {} reqwest({ ur ...
- Python 命令总结
本章内容 pip pip install -r requirement.py(里面写入需要安装的包的名字) pip install django==1.9 #需要安装那个版本 P ...
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- Careercup - Microsoft面试题 - 23123665
2014-05-12 07:44 题目链接 原题: Given an array having unique integers, each lying within the range <x&l ...
- 53、listview、expandableListview如何选中时保持高亮?
一.listView被选中后保持高亮 70down voteaccepted To hold the color of listview item when you press it, include ...
- IOS笔记051-手势使用
UIGestureRecognizer 利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 UIGestureRecognizer是一个抽象类,定义了所有手势 ...
- 动态生成的chosen实现模糊查询
$('select', newTr).chosen({ width: '100%', search_contains: true }); //初始化复制行下拉框