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. Linux5个数据段

        进程(执行的程序)会占用一定数量的内存,它或是用来存放从磁盘载入的程序代码,或是存放取自用户输入的数据等等.不过进程对这些内存的管理方式因内存用途不一而不尽相同,有些内存是事先静态分配和统一回 ...

  2. git 使用规范

    git使用资料: https://github.com/peak-c/my-git 公司内部使用开发规范: 一. 代码库介绍 个人开发库(git@gitlab.adrd.sohuno.com:sper ...

  3. Redis实现之数据库(三)

    过期键删除策略 在Redis实现之数据库(二)一小节中,我们知道了数据库键的过期时间都保存在过期字典中,又知道了如果根据过期时间去判断一个键是否过期,现在剩下的问题是:如果一个键过期了,那么它什么时候 ...

  4. Eclipse启动错误:A Java Runtime Environment(JRE) or Java Development Kit(JDK) must be available……

    确保Jdk,Jre都安装完成并且环境变量配置无误的情况下,自动Ecplise报错如下: A Java Runtime Environment (JRE) or Java Development Kit ...

  5. 转载 hadoop 伪分布安装

    一. 概要        经过几天的调试,终于在Linux Cent OS 5.5下成功搭建Hadoop测试环境.本次测试在一台服务器上进行伪分布式搭建.Hadoop 伪分布式模式是在单机上模拟 Ha ...

  6. Struts2请求流程

    1. 一个请求在Struts2框架中的处理步骤: a) 客户端初始化一个指向Servlet容器的请求: b) 根据Web.xml配置,请求首先经过ActionContextCleanUp过滤器,其为可 ...

  7. nyoj 题目5 Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 Given two strings A and B, whose alp ...

  8. 多线程(继承Thread)

    /** *Thread的常用方法 *1.start(),启动线程再执行run方法 *2.run():子线程要执行的代码放入run()方法中 *3.currentThread()静态的,调取当前线程,返 ...

  9. 用canvas绘制android机器人

    直接上代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. [BZOJ1066][luogu_P2472][SCOI2007]蜥蜴

    [BZOJ1066][luogu_P2472][SCOI2007]蜥蜴 试题描述 在一个 \(r\) 行 \(c\) 列的网格地图中有一些高度不同的石柱,一些石柱上站着一些蜥蜴,你的任务是让尽量多的蜥 ...