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 : ...
随机推荐
- winform调用jar包
因为工作需要,需要做一个数据上传的程序,客户规定的是:数据接口采用http连接,采用JSON-RPC轻量级远程调用协议.所以决定用winform做一个管理界面(其中还包括其他的功能),java完成数据 ...
- 最近用到的 sql 统计操作
统计操作 1.分组统计group by select id,name,count(*) as '总数' from test1 group by id,name --group by 分组 ...
- for之于while的优势
前言 for与while各有功效,下面就只列举for之于while的优势有哪些 优势 1.循环中提供了特殊的机会来将变量的作用域最小化.(无论是传统的还是for-each形式的)for循环,都允许声明 ...
- 【爬坑】MySQL 无法启动
[说明] 启动 MySQL 的时候出现以下错误 [解决] 在网上查到了遇到相关问题的人的解决方法,参考连接 Mysql启动报错 原因是 MySQL 服务没启动,开启就好了. 最后分析之所以服务没开启, ...
- div中文本水平居中,垂直居中
div: text-align=center; hight=100px; line-hight=100px;(行高需要和高度设置成一样)
- 详解动态规划(Dynamic Programming)& 背包问题
详解动态规划(Dynamic Programming)& 背包问题 引入 有序号为1~n这n项工作,每项工作在Si时间开始,在Ti时间结束.对于每项工作都可以选择参加与否.如果选择了参与,那么 ...
- 处理AsyncTask的内存泄漏问题
强引用AsyncTask导致了内存泄漏如下图 1.原因:activity销毁之后,AsyncTask线程可能依旧在执行,导致内存泄漏. 2.解决方法:查了一下大概有两个,一个是将函数声明为static ...
- 阿里八八Alpha阶段Scrum(6/12)
今日进度 叶文滔: 修复了无法正确判断拖曳与点击的BUG,并且成功连接添加界面. 会议内容 会议照片 明日安排 叶文滔: 继续完善按钮功能 王国超: 继续攻克日程界面显示存在的BUG 俞鋆: 继续进行 ...
- volatile和synchronized的区别与联系[转]
volatile是一个变量修饰符,而synchronized是一个方法或块的修饰符.所以我们使用这两种关键字来指定三种简单的存取变量的方式. int i1; ...
- Arcgis for Js之加载wms服务
概述:本节讲述Arcgis for Js加载ArcgisServer和GeoServer发布的wms服务. 1.定义resourceInfo var resourceInfo = { extent: ...