一.题目链接:

  https://leetcode.com/problems/letter-combinations-of-a-phone-number/

二.题目大意:

  给定一段数字字符串,其中每个数字字符对应了如下的字母字符,求出输入字符串对应的所有可能的字母字符串集合。

  

  例如,输入数字字符串"23",其对应的所有可能的字母字符串集合为 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

三.题解:

  这道题目直接根据题意来做即可,相当于每个数字字符对应若干个字母字符,把这些所有可能的字母组合存储起来即可,本体代码如下 (本题的思想不难理解,关键是这个实现的过程,需要仔细琢磨下):

class Solution {
public:
vector<string> letterCombinations(string digits) {
string dic[] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
vector<string> res;
if(digits == "")
return res;
res.push_back("");//初始化结果数组
int d_len = digits.size();
for(int i = 0; i < d_len; i++)
{
vector<string> tmp;//用于扩充结果的中间变量
string str = dic[digits[i] - '0'];
if(str == "")
continue;
for(int j = 0 ; j < str.size(); j++)
for(int k = 0; k < res.size(); k++)
tmp.push_back(res[k] + str[j]);//对res数组的每个元素附上新的字母字符
res = tmp;//每次中间处理完之后,交换结果
}
return res;
}
};

注意:

1.当输入的数字字符串为空时,最终返回的字符字符串集合为空,所以这里需要特判一下。

2.初始时res数组必须有一个“”,这样做的目的是为了方便后续将res数组进行扩充,因为后续的扩充操作实际上就是在res数组原有的每个元素基础上附加上新的字母字符,并且这个过程由tmp数组来实现,最终将tmp数组的结果赋值给res数组作为最终的结果。

LeetCode——17. Letter Combinations of a Phone Number的更多相关文章

  1. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

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

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

  3. [leetcode 17]Letter Combinations of a Phone Number

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

  4. Java [leetcode 17]Letter Combinations of a Phone Number

    题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...

  5. Leetcode 17.——Letter Combinations of a Phone Number

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

  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. [LeetCode] 17. Letter Combinations of a Phone Number ☆☆

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

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

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

  9. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

随机推荐

  1. Python成长之路【第二篇】Python基础之数据类型

    阅读目录 简介 1 什么是数据? x=10,10是我们要存储的数据 2 为何数据要分不同的类型 数据是用来表示状态的,不同的状态就应该用不同的类型的数据去表示 3 数据类型 数字(整形,长整形,浮点型 ...

  2. VIM学习二: VIM配置代码及效果图

    vim学习及插件 参见:http://www.cnblogs.com/caixu/p/6337926.html .vimrc配置 "***************************** ...

  3. web 自定义标签

    Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签).而自定义标签的好处,就是在大型web开发的时候,可以封装 ...

  4. L2-014 列车调度 (25 分)

    L2-014 列车调度 (25 分)   火车站的列车调度铁轨的结构如下图所示. 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选择 ...

  5. C#字符串的CompareTo比较,让我疑惑的地方

    在学习选择排序算法的时候,用到CopareTo方法.由于比较的数字,是自己随意输入的. 当我输入字符串“8”,它和字符串“16”比较时候. string str1 = "8"; s ...

  6. Python第十课学习

    Python第十课学习 www.cnblogs.com/yuanchenqi/articles/5828233.html 函数: 1 减少代码的重复 2 更易扩展,弹性更强:便于日后文件功能的修改 3 ...

  7. 停止node进程和查看react-native-cli

    taskkill /f /t /im node.exe which react-native

  8. Python Redis中Scan遇到问题

    在项目启动中需要删除redis原先相同key储存的值,所以使用scan_iter来便利相关的key,并删除. 这里需要注意两个性能问题 1. scan_iter的模糊匹配的过滤器要正确,否则会带来很多 ...

  9. div 隐藏显示各种例子

    <html><head><title>jquery show()</title><script type="text/javascrip ...

  10. js 数字随机滚动(数字递增) 每日凌晨回到原点,重新计算

    html: <div class="textMon"> <!--<img src="./img/20180830160315.png" ...