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 ...
随机推荐
- 51nod 1105 二分答案法标准题目
二分答案法例题,用于练习二分答案的基本思想非常合适,包括了思维方式转换的内容(以前我们所做的一直是利用二分法求得数组元素对应指针之类,但是现在是直接对答案进行枚举). 思路是:首先对输入数组进行排序, ...
- A JavaScript Image Gallery
childNodes property: The childNodes property is a way of getting information about the children of ...
- CQRS之旅——旅程5(准备发布V1版本)
旅程5:准备发布V1版本 添加功能和重构,为V1版本发布做准备. "大多数人在完成一件事之后,就像留声机的唱片一样,一遍又一遍地使用它,直到它破碎,忘记了过去是用来创造更多未来的东西.&qu ...
- OpenCV学习笔记(二) cv::Mat
部分内容转自:OpenCV Tuturial,ggicci 在OpenCV Tuturial中可查看Mat的初始化与打印方法. Mat本质上是由两个数据部分组成的类: 矩阵头(包含矩阵尺寸,存储方法, ...
- 网络流24题:P2762 太空飞行计划问题
P2762 太空飞行计划问题 题目背景 题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,E ...
- Git的安装及常用操作
一.Git的安装 1.下载Git,官网地址为:https://git-scm.com/downloads. 2.下载完成之后,双击目录进行安装 3.选择安装目录 4.选择组件,默认即可 5.设 ...
- leetcode 【 Remove Element 】python 实现
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- plsql编程
ORACLE PL/SQL编程详解 SQL语言只是访问.操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发.PL /SQL是一种高级数据库程序设计语言,该 ...
- 替换掉 in的like操作
select * from t_unit where '410300060025,410300004005,410300998851,' like '%'+ltrim(rtrim(unitcode)) ...
- redis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is configured to save RDB snapshots
最近在学习Redis ,在写test测试的时候碰到这个报错: redis.clients.jedis.exceptions.JedisDataException: MISCONF Redis is c ...