LeetCode——17. Letter Combinations of a Phone Number
一.题目链接:
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的更多相关文章
- 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 ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- [leetcode 17]Letter Combinations of a Phone Number
1 题目: Given a digit string, return all possible letter combinations that the number could represent. ...
- Java [leetcode 17]Letter Combinations of a Phone Number
题目描述: Given a digit string, return all possible letter combinations that the number could represent. ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- [LeetCode] 17. Letter Combinations of a Phone Number ☆☆
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)
题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...
随机推荐
- Representations of graphs
We can choose between two standard ways to represent a graph as a collection of adjacency lists or a ...
- Android选择多图上传
大概是这么个效果,类似与微信朋友圈选择图片的效果,如下图所示 首先,图片预览的这个界面我用的是GridView,当然也可以使用GridLayout,根据需求制定行列即可.同时重写适配器实现末尾是添加图 ...
- 【温故知新】HTTP请求GET和POST的用法和区别
转自http://www.w3school.com.cn GET的使用 请注意,查询字符串(名称/值对)是在 GET 请求的 URL 中发送的: /test/demo_form.asp?name1=v ...
- servlet之servlet容器(一)
1.servlet容器 ·servlet容器为javaweb应用提供运行时环境,负责管理servlet和jsp的生命周期以及管理它们的共享数据 ·servlet容器中的文件目录结构 ·tomcat是一 ...
- C# 对结构体和指针的使用
//结构体的定义 [StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] pub ...
- Python heapq模块
注意,默认的heap是一个小顶堆! heapq模块提供了如下几个函数: heapq.heappush(heap, item) 把item添加到heap中(heap是一个列表) heapq.heappo ...
- hyperscan应用参数
>>hs_compile_ext_multi 使用额外的参数编译表达式, 额外的参数包括: MIN_OFFSET 距离开始的最小偏移开始匹配 MAX_OFFSET 距离开始的最大偏移结束匹 ...
- Centos中安装gitlab
安装依赖: sudo yum install curl openssh-server openssh-clients postfix cronie sudo service postfix start ...
- 小括号转义 '\\s'
select split("2405F5 (base 16) Integrated Device Technology (Malaysia) Sdn. Bhd.","\\ ...
- mysql 中文支持
show variables like 'character%'; SHOW VARIABLES LIKE 'collation_%'; recommend to use utf8mb4 inste ...