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 ***的更多相关文章

  1. 【leetcode】Letter Combinations of a Phone Number

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  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. [leetcode 17]Letter Combinations of a Phone Number

    1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...

  4. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  5. 【leetcode】 Letter Combinations of a Phone Number(middle)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. 【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 ...

  7. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  8. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  10. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

随机推荐

  1. Flask学习笔记:数据库ORM操作MySQL+pymysql/mysql-python+SQLAlchemy/Flask-SQLAlchemy

    Python中使用sqlalchemy插件可以实现ORM(Object Relationship Mapping,模型关系映射)框架,而Flask中的flask-sqlalchemy其实就是在sqla ...

  2. CMD终端关于pip报错,scrapy报错的一种处理方法

    CMD终端关于pip报错,scrapy报错的一种处理方法 如果在终端输入pip,或scrapy,报如下错误: Fatal error in launcher: Unable to create pro ...

  3. 各种友(e)善(xin)数论总集(未完待续),从入门到绝望

    目录 快速幂 扩展欧几里得 GCD 扩展欧几里得 同余系列 同余方程 同余方程组 一点想法 高次同余方程 BSGS exBSGS 线性筛素数 埃式筛 欧拉筛 欧拉函数 讲解 两道水题 法雷级数 可见点 ...

  4. Roads in the North POJ - 2631

    Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...

  5. HDU:1358-Period

    Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Desc ...

  6. 读《深入理解jvm虚拟机》之长期存活对象进入老年代,有感!!!!

    关于这一段代码 有几个不是让人很理解的地方,我一一说来. 1.Desired survivor size 524288 bytes 关于这个512KB空间是怎么来的,JVM有这样一个参数: -XX:T ...

  7. input框中的必填项之取消当前input框为必填项

    html5新增了一个required属性,可以使用这个属性对文本框设置必填项,直接在input文本框上添加required即可 . 效果如图:   

  8. SXCPC2018 nucoj2004 国王的怪癖

    可持久化trie.考场上我脑补了一个trie树合并也A了 #include <iostream> #include <cstring> #include <cstdio& ...

  9. 纯js国际化(i18n)

    i18n,是internationalization单词的简写,中间18个字符略去,简称i18n,意图就是实现国际化,方便产品在不同的场景下使用 目标:可以点击切换语言或者ChangeLanguage ...

  10. 【Unique Paths II】cpp

    题目: Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. H ...