题意

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)的更多相关文章

  1. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  2. [LeetCode]Letter Combinations of a Phone Number题解

    Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...

  3. [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  4. LeetCode——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  5. [LeetCode] Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  6. [LeetCode] Letter Combinations of a Phone Number(bfs)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. [LeetCode] Letter Combinations of a Phone Number 回溯

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

  9. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. SQLSERVER中的资源调控器

    SQLSERVER中的资源调控器 转载自: http://wenku.baidu.com/view/0d92380cf78a6529647d5375.html http://www.cnblogs.c ...

  2. MySQL应用架构优化-实时数据处理

    1.1. 场景 在和开发人员做优化的时候,讨论最多的应该是结合应用场景编写出合适的SQL.并培训开发应该如何编写SQL让MySQL的性能尽量好.但是有一些的场景对于SQL的优化是行不通的. 打个比方, ...

  3. ajax status 错误

    status **:未被始化 status **:请求收到,继续处理 status **:操作成功收到,分析.接受 status **:完成此请求必须进一步处理 status **:请求包含一个错误语 ...

  4. 向kindle传送文件

    自从有了kindle以后,只要不是pdf的电子书,基本上都用kindle来看了,毕竟水墨屏还是很护眼的,但是这就带来了向kindle传输文件的问题 绝大部分的电子书用邮件推送就可以解决了,但是还有一些 ...

  5. Linux之JDK1.8的安装

    这个最基础的,但是老是查了一次又查,干脆记起来吧. 一.下载jdk8 地址:http://www.oracle.com/technetwork/java/javase/downloads/index. ...

  6. linux shell编程进阶学习(转)

    第一节:基础 ls -lh  ——可以用户友好的方式看到文件大小 file 文件名 ——查看文件类型 stat 文件名 ——查看文件当前状态 man 命令/函数名 ——查看详细的帮助文档 man中看某 ...

  7. Servlet 核心接口

    在Servlet体系结构中,除了用于实现Servlet的Servlet接口.GenericServlet类和HttpServlet类外,还有一些辅助Servlet获取相关资源信息的重要接口,了解这些接 ...

  8. python 使用csv 文件写入 出现多余空行数据解决方案

    因为csv.writerow() 方法会造成读取时每条数据后多一条空数据 解决方案如下: 分为两种情况 python2 和 python3 先说python2版本 with open('xxx.csv ...

  9. Java中Map根据键值(key)或者值(value)进行排序实现

    我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...

  10. python第四十课——构造函数

    1.动态给对象添加属性: 在对象创建完毕后,单独为其添加需要的属性:可以理解为:私人定制 [注意]: 添加的属性只有此对象能够使用,别的对象如果用了,直接报错; 2.构造函数/构造方法/构造器: 格式 ...