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 : ...
随机推荐
- jQuery中使用attribute,prop获取,设置input的checked值【转】
1.prop方法获取.设置checked属性 当input控件checkbox设置了checked属性时,无论checked=”“或 checked=”checked”,$(obj).prop(“ch ...
- Quick and Easy Installation of Oracle Database 12c on Oracle Linux in Oracle VM VirtualBox
发贴人 Sergio-Oracle 于2018-4-18 23:10:15在Oracle Linux Introduction How Does This Work? Requirements Bef ...
- 转:C#常用的集合类型(ArrayList类、Stack类、Queue类、Hashtable类、Sort)
C#常用的集合类型(ArrayList类.Stack类.Queue类.Hashtable类.Sort) .ArrayList类 ArrayList类主要用于对一个数组中的元素进行各种处理.在Array ...
- Java重要类详解之ArrayList类
https://blog.csdn.net/shengmingqijiquan/article/details/52634640 一.ArrayList概述 ArrayList 是一个数组队列,相当于 ...
- [IDEA_5] IDEA 集成 Scala
0. 说明 在 IDEA 中集成 Scala 1. IDEA 集成 Scala 1.1 安装 Scala 插件 Ctrl + Alt + S 进入设置 依次选中 Settings --> P ...
- 解决Win10无法安装.Net Framework 3.5,错误代码0x800F081F
重新安装了一遍Win10,但是不知怎的无法安装.net framework 3.5,即便是下载离线安装包也没法用. 网上有人说需要使用win10的ISO文件,个人感觉太麻烦,在这里分享一个很方便的操作 ...
- fedora27安装后的配置工作(持续更新)
换源 没什么可说的,安装后更换国内软件源是必须做的事,推荐更换阿里的镜像源.换源教程 添加epel源 EPEL (Extra Packages for Enterprise Linux)是基于Fedo ...
- Vue.js Is Good, but Is It Better Than Angular or React?
Vue.js is a JavaScript library for building web interfaces. Combining with some other tools It also ...
- 小米3系统计算器自己定义开关控件-MySwitchView
1.前言 在android4.0以后,有switch控件.相似于iPhone上面滑块的效果.可是仅仅能用在4.0以后的系统中.之前的平台.就无法使用这种控件. 近段时间.看到了 ...
- Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析
UIl与Volley一样是非常古老的框架,UIL实现了从网络获取图片,对图片进行缓存,以及根据个性化的设置来将图片加载到ImageView上. 这篇文章 主要分析UIl在初始化配置的源码 UIL初始化 ...