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 OPM 车间发料事务处理
--车间发料事物处理 --created by jenrry DECLARE l_iface_rec inv.mtl_transactions_interface%ROWTYPE; l_iface_l ...
- Cisco HSRP 配置方法(热备份路由协议)配置实例
转裁于51CTO.http://www.mamicode.com/info-detail-862350.html HSRP----热备份路由协议 思科私有协议,与VRRP 虚拟路由协议 相近,(国际标 ...
- Huawei DHCP 中继配置实例
配置DHCP中继示例 组网需求 如图1,DHCP客户端所在的网段为10.100.0.0/16,而DHCP服务器所在的网段为202.40.0.0/16.需要通过带DHCP中继功能的设备中继DHCP报文, ...
- CameraAPI中的 自定义照相功能
前几天的项目需要使用CameraAPI自己定义照相机,之前用过的二维码也要自己写底层代码,于是总结一下使用CameraAPI的几点事项.现在由于JDK7.0及其以上版本的官方文档已经不再推荐使用cam ...
- [ML学习笔记] 决策树与随机森林(Decision Tree&Random Forest)
[ML学习笔记] 决策树与随机森林(Decision Tree&Random Forest) 决策树 决策树算法以树状结构表示数据分类的结果.每个决策点实现一个具有离散输出的测试函数,记为分支 ...
- 1.Redis安装(Linux环境)
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.Redis安装 使用的最新版本为 3.2.9,下载并安装: wget http://download.re ...
- CSS鼠标经过另类做法
HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- Python程序的执行原理
1. 过程概述 Python先把代码(.py文件)编译成字节码,交给字节码虚拟机,然后虚拟机一条一条执行字节码指令,从而完成程序的执行. 2. 字节码 字节码在Python虚拟机程序里对应的是PyCo ...
- [SDOI2008]洞穴勘测
嘟嘟嘟 写完lct的板儿后觉得这就是一道大水题. 连pushup都不用. 不过还是因为一个zz的错误debug了一小会儿(Link的时候连出自环--) 还有一件事就是Cut的时候判断条件还得加上,因为 ...
- Ubuntu16.04安装Zabbix3.2(快速安装教程)
ubuntu16.04下zabbix安装和配置 上面这篇虽然是一样的,但是针对的版本有差异,版本差异,安装方式也就有差异,对要求的环境可能有所不同. 今天帮同事安装zabbix,按照如下几个步骤,就完 ...