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. relu函数是否存在梯度消失问题以及relu函数的死亡节点问题

    relu函数是否存在梯度消失问题以及relu函数的死亡节点问题 存在,在小于的时候,激活函数梯度为零,梯度消失,神经元不更新,变成了死亡节点. 出现这个原因可能是因为学习率太大,导致w更新巨大,使得输 ...

  2. Labyrinth POJ - 1383

    Labyrinth POJ - 1383 The northern part of the Pyramid contains a very large and complicated labyrint ...

  3. Java的多态性Polymorphism

    原文地址:http://www.cnblogs.com/jack204/archive/2012/10/29/2745150.html Java中多态性的实现 什么是多态 面向对象的三大特性:封装.继 ...

  4. Linux下安装nginx,以及启动和停止

    1.安装 安装nginx之前,首先确保系统已经安装了依赖:g++.gcc.openssl-devel.pcre-devel和zlib-devel软件 yum install gcc-c++ yum - ...

  5. MySQL之Schema与数据类型优化

    选择优化的数据类型 MySQL支持的数据类型非常多,选择正确的数据类型对于获得高性能至关重要.不管存储哪种类型的数据,下面几个简单的原则都有助于做出更好的选择: 更小的通常更好一般情况下,应该尽量使用 ...

  6. 【Median of Two Sorted Arrays】cpp

    题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...

  7. Map-Reduce基础

    1.设置文件读入分隔符 默认按行读入; 按句子读入 : conf1.set("textinputformat.record.delimiter", "."); ...

  8. 在Myeclipse8.5中安装findbugs方法

    step 1:首先从官网下载findbugs插件:http://downloads.sourceforge.net/project/findbugs/findbugs%20eclipse%20plug ...

  9. Map 中的EntrySet() ,Map的遍历

      我们循环Map时一般用到EntrySet(),EntrySet() 返回的时Set集合(Set<Map.Entry<K, V>>). 那么这里的有Map.Entry< ...

  10. ALPHA 冲刺(一)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:丹丹 组员7:家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示组内 ...