给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:

尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

搜索回溯

class Solution {
public:
vector<string> res;
vector<vector<char> > hash;
vector<string> letterCombinations(string digits)
{
int len = digits.size();
if(len == 0)
return res;
hash = vector<vector<char> >(10, vector<char>(5, 0));
int cnt = 0;
for(int i = 2; i <= 9; i++)
{
int boundary;
if(i == 7 || i == 9)
boundary = 4;
else
boundary = 3;
for(int j = 0; j < boundary; j++)
{
hash[i][j] = cnt + 'a';
cnt++;
}
}
DFS(0, "", digits);
return res;
} void DFS(int cnt, string str, string digits)
{
if(cnt == digits.size())
{
res.push_back(str);
return;
}
int x = digits[cnt] - '0';
for(int i = 0; i < hash[x].size(); i++)
{
if(hash[x][i] == 0)
break;
char temp = hash[x][i];
DFS(cnt + 1, str + temp, digits);
}
}
};

Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合的更多相关文章

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

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

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

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

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

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  4. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  5. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

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

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  7. 017 Letter Combinations of a Phone Number 电话号码的字母组合

    给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...

  8. 算法练习--LeetCode--17. Letter Combinations of a Phone Number

    Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, ret ...

  9. LeetCode17 Letter Combinations of a Phone Number

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

随机推荐

  1. Windows操作系统下创建进程的过程

    进程(Process)是具有一定独立功能的程序关于某个数据集合上的一次运行活动,是系统进行资源分配和调度的一个独立单位.程序只是一组指令的有序集合,它本身没有任何运行的含义,只是一个静态实体.而进程则 ...

  2. kubeadm安装Kubernetes 1.15 实践

    原地址参考github 一.环境准备(在全部设备上进行) 3 台 centos7.5 服务器,网络使用 Calico. IP地址 节点角色 CPU 内存 Hostname 10.0.1.45 mast ...

  3. centos 以太坊多节点私链搭建

    环境  centos 7   搭建 3 个节点的 私链. 第一步 安装 一些依赖的 工具 yum update -y && yum install git wget bzip2 vim ...

  4. java流对象

    Java和C++都是静态类型的面向对象编程语言 stream结尾都是字节流,reader和writer结尾都是字符流 区别: 就是读写的时候一个是按字节读写,一个是按字符. 实际使用通常差不多. 在读 ...

  5. 在熟练使用2B铅笔前,请不要打开Axure

    在互联网产品领域,Axure已成为产品经理.产品设计师以及交互设计师的必备工具,从某种程度讲,Axure帮助我们建立低保真模型,便于与用户的需求验证,也帮助我们构思交互细节,使前端和开发人员更容易理解 ...

  6. Spring注解驱动(下)

    9.@PropertySource 加载配置文件 在xml中 我们加载property配置文件,是使用 <context:property-placeholder location=" ...

  7. vue 可复用swiper以及scoped样式穿透(可以不受scoped的限制来修改样式)

    参考: https://blog.csdn.net/dwb123456123456/article/details/82701740https://blog.csdn.net/u014027876/a ...

  8. Activiti流程变量

    流程变量在整个工作流中扮演很重要的作用 例如:请假流程中有请假天数.请假原因等一些参数都为流程变量的范围.流程变量的作用域范围是流程实例.也就是说各个流程实例的流程变量是不相互影响的. 添加流程变量 ...

  9. np一些基本操作2

    import numpy as nparr1 = np.arange(32).reshape(8,4)print(arr1)arr1 = arr1.reshape(-1);print(arr1)arr ...

  10. 为互联网业务而生:阿里云全球首发云Cassandra服务!

    引言:十年沉淀.全球宽表排名第一.阿里云首发云Cassandra服务 ApsaraDB for Cassandra是基于开源Apache Cassandra,融合阿里云数据库DBaaS能力的分布式No ...