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< ...
随机推荐
- kafka学习笔记整理01
Apache Kafka 消息中间件->流式数据处理平台 特点: 高可靠性(分布式.分区.复制和容错).高吞吐量.持久化 消息模式:1)队列式(或点对点):在点对点消息系统中,消息被保存在队列中 ...
- 玩转BLE(1)_Eddystone beacon
1. 前言 你相信两条命令就可以把自己的破手机变成一个Beacon节点吗?不相信的话就接着往下看吧. 通过前几篇“蓝牙协议分析”相关的文章,特别是“蓝牙协议分析(3)_蓝牙低功耗(BLE)协议栈介绍” ...
- 牛客网PAT乙级(Basic Level)真题-数字分类 (20)
题目描述 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4 ...
- Netty 中 EventLoopGroup 的创建
本文是基于 Netty 4.1.6.Final 的源码来分析的. 在分析源码之前做一些准备工作: 先熟悉一下 IDEA 的几个快捷键,能极大的提高我们查看源码的效率: Ctrl + Alt + B:用 ...
- modelSIM仿真ROM核报错
用Quartus Prime 15.1调用modelSIM进行RTL仿真,modelSIM的transcript框框总是有Failed to Open Readmem File提示. 错误原因:IP核 ...
- centos7部署asp.net core 应用程序
一.运行环境 1.腾讯云与主机一台 2.操作系统是centos7 3.安装.net core sdk 4.安装node及其pm2(安装过程) rpm -Uvh https://packages.mic ...
- Visual C++ 6.0中互换两个数字
就好比如两杯水,需要三个杯子将这两杯水互换.互换数字就要用一个中间变量. # include <stdio.h> int main(void) { int i; int j; int k; ...
- 图的深度优先遍历(DFS)—递归算法
实验环境:win10, DEV C++5.11 实验要求: 实现图的深度优先遍历 实验代码: #include <iostream> #define maxSize 255 #includ ...
- 小妖精的完美游戏教室——东方PROJECT,同人,th12灵梦A
╮(╯▽╰)╭没办法,小妖精Balous也很讨厌学院化的教育呀,一点意义都没有. 这次就上传东方地灵殿灵梦A逻辑部分的核心代码吧,估计连老师都看不懂.动画部分的代码就不放上来了. //======== ...
- tornado--初识tornado
tornado的第一个程序 import tornado.ioloop import tornado.web class Index(tornado.web.RequestHandler): def ...