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< ...
随机推荐
- winform程序读取和改写配置文件App.config元素的值
winform程序读取和改写配置文件App.config元素的值 2016-05-16 17:49 by newbirth, 2412 阅读, 0 评论, 收藏, 编辑 1 2 3 4 5 6 7 & ...
- MySQL最基本的概念梳理
本文根据<MySQL必知必会>(Ben Forta著,2009)整理,基于MySQL4.1-5,可作为深入研究MySQL之前的漱口篇.(基本语句.正则表达式.联结.全文本搜索.增删改查.存 ...
- UML作业第三次:分析《书店图书销售管理系统》,绘制类图
一. 类图语法学习小结(类间关系的表示方法) 1.抽象类和接口 我们用关键字abstract或abstract class来定义抽象类(抽象类用斜体显示).也可以使用interface,annotat ...
- git特殊命令
1.git追踪远程分支,该命令使用Tab不会自动补全 git branch --set-upstream-to=远程分支名(origin/xxx) 2.从远程分支创建本地新分支 git checkou ...
- C# copy() 与 Clone()区别
copy() 与 Clone()都创建了一个新对象 DataTable dt=new DataTable();DataTable dtcopy=dt.copy(); //copy复制的是值和 ...
- Netty 服务端:新连接接入
本文主要分析服务端新连接的接入过程,主要分为以下 3 各步骤: select 操作: processSelectedKeys 操作. 1. select 操作 在分析 select 操作前,先要回顾一 ...
- 自动保存python一个项目的需求文件
# 保存python3环境下安装的所有模块 $ pip3 freeze > requirements.txt # 保存当前项目中所依赖的模块 $ pipreqs ./ 依赖模块保存在:requi ...
- cocos2dx取真正随机数
由于c++的随机数其实是用了一张随机表,所以不是真正意义上的随机,cocos2dx中操作的时候会发现每次 重新获取都会得到同样的值,那么解决办法采用置随机数种子,利用时间函数(时间唯一性),操作如下 ...
- mysql count 主键之坑
https://www.2cto.com/database/201508/433975.html
- 第七次实验:CC2530平台上多跳通信的TinyOS编程
module P2MM { uses interface Boot; uses interface Timer<TMilli> as Timer0; uses interface Spl ...