LeetCode Letter Combinations of a Phone Number (DFS)
题意
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
给定数字,输出所有可能的字母组合
解法
可以先将数字对应的字母存好,然后遍历输出就可以。只不过不用递归来写好像比较麻烦,这里用了一个DFS。
class Solution
{
public:
vector<string> letterCombinations(string digits)
{
static vector<vector<char>> table = {
{},{},
{'a','b','c'},
{'d','e','f'},
{'g','h','i'},
{'j','k','l'},
{'m','n','o'},
{'p','q','r','s'},
{'t','u','v'},
{'w','x','y','z'}
};
vector<string> rt;
string temp;
dfs(digits,table,rt,0,digits.size(),temp);
return rt;
}
void dfs(string digits,vector<vector<char>> table,vector<string> & ans,int k,int length,string temp)
{
if(k >= length && temp.size())
{
ans.push_back(temp);
return;
}
for(int i = 0;i < table[digits[k] - '0'].size();i ++)
{
temp += table[digits[k] - '0'][i];
dfs(digits,table,ans,k + 1,length,temp);
temp.pop_back();
}
}
};
LeetCode Letter Combinations of a Phone Number (DFS)的更多相关文章
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] Letter Combinations of a Phone Number 回溯
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
- leetcode Letter Combinations of a Phone Number python
class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...
随机推荐
- Oracle EBS INV 更新物料慢
失效 ICX SELECT FA.APPLICATION_SHORT_NAME, FA.APPLICATION_ID, PI.STATUS, PI.PRODUCT_VERSION, PI.PATCH_ ...
- python自学——文件修改
#如何修改文件,我们知道文件因为在磁盘上已经有储存了,后面要更新或修改,只能在在原来文件后面追加使用f=open("wenjian_name","r+",enc ...
- MySQL案例08:MySQL Scheduler Events带来的风险
定时任务是我们开发.运维人员经常用到的,比如cron,job,schedule,events scheduler等都是为了方便我们重复执行某项工作而无需人工参与而设计,这里我要说的是MySQL数据库本 ...
- Software Engineer’s path to the best annual performance review
http://michaelscodingspot.com/2017/06/04/software-engineers-path-best-annual-performance-review/ How ...
- Hash Tables
哈希表 红黑树实现的符号表可以保证对数级别的性能,但我们可以做得更好.哈希表实现的符号表提供了新的数据访问方式,插入和搜索操作可以在常数时间内完成(不支持和顺序有关的操作).所以,在很多情况下的简单符 ...
- Beta版本项目计划
小队名称:PHILOSOPHER 小组成员 [组长]金盛昌(201421122043).刘文钊(20142112255).陈笑林(201421122042) 张俊逸(201421122044).陈志建 ...
- 配置好jdk后,cmd编写java -version还是报找不到jdk
找了好长时间没查出问题在哪,配置的都没有问题,最后搜了搜网上发现,jdk的这两行得在最上面才行.
- rpm安装MySQL5.5后配置,在centos5上;mysql编译安装在centos6.5上;
[1] 没有/etc/my.cnf: rpm包安装的MySQL是不会安装/etc/my.cnf文件的:处理:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf [2 ...
- [转]在C++中容易出现的#error No Target Architecture
项目环境:win 7 64位,编译环境:VS2013 最近在写C++的项目,发现了自己很多不会的地方,这也使得我在C++中的成长变得比较快,下面我就说说我自己在写项目是遇到的一些问题,希望可以帮到一些 ...
- 使用Tensoflow实现梯度下降算法的一次线性拟合
# Author Qian Chenglong import tensorflow as tf import numpy as np #生成100个随机数据点 x_date=np.random.ran ...