给定一个仅包含数字 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. 深入浅出 Java Concurrency (25): 并发容器 part 10 双向并发阻塞队列 BlockingDeque[转]

    这个小节介绍Queue的最后一个工具,也是最强大的一个工具.从名称上就可以看到此工具的特点:双向并发阻塞队列.所谓双向是指可以从队列的头和尾同时操作,并发只是线程安全的实现,阻塞允许在入队出队不满足条 ...

  2. 左神算法进阶班4_2累加和为aim的最长子数组

    [题目] 给定一个数组arr,和一个整数aim,求在arr中,累加和等于num的最长子数组的长度 例子: arr = { 7,3,2,1,1,7,7,7 } aim = 7 其中有很多的子数组累加和等 ...

  3. js时间操作getTime(),ios移动端真机上返回显示NAN

    ios移动端,js时间操作getTime(),getFullYear()等返回显示NaN的解决办法及原因 在做移动端时间转化为时间戳时,遇到了一个问题,安卓手机上访问时,能拿到时间戳,从而正确转换时间 ...

  4. 给java mongodb 官方driver 增加bean 操作

      mongodb官方的java driver不支持直接插入java bean,只能使用DbObject的Key,Value形式进行insert,update,(c# mongodb官方driver类 ...

  5. Activiti流程定义部署、删除

    1.部署流程定义 部署流程定义也可以认为是增加流程定义.  首先创建流程引擎对象(公用的方法)   private ProcessEngine processEngine = ProcessEngin ...

  6. Codeforces 486D. Valid Sets

    D. Valid Sets time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. 计算机程序是怎么通过cpu,内存,硬盘运行起来的?

    虽然以前知道计算机里有CPU,内存,硬盘,显卡这么些东西,我还真不知道这些东西是怎么协作起来完成一段程序的,能写出程序却不懂程序,也不会向别人解释他们的关系,所以特意总结了一下,写的比较浅显,和我一样 ...

  8. SQL Server作业的备份

    作业备份,不是备份数据库,是备份作业.我的方法是把作业导出成文件备份起来,因为当你服务器维护的多了的时候很多你的作业 就很成问题,很麻烦.最好能够作业实现同步,这个也是第一步,保存成文件,之后个人设想 ...

  9. DVWA 之medium级别sql注入

    中级注入的提交方式从get请求改为post请求,可以用burp抓包注入或抓注入点 1 .  判断是否有注入 sqlmap -u "http://192.168.242.1/dvw/vulne ...

  10. 【DM642学习笔记三】flash的烧写

    ICETEK-DM642-PCI板上的29L008B芯片提供了8M位的Flash空间(访问地址空间是CE1,90000000h~90080000h).主要用于自启动功能和存储FPGA的配置数据. 一. ...