一.题目链接:

  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. PHP判断引入文件是否引入成功

    $included_files = get_included_files(); foreach ($included_files as $filename) { echo "$filenam ...

  2. python之路之简单介绍:

    python介绍: a. python 基础 - 基础 - 基本的数据类型 - 函数 - 面向对象 python 安装 python 安装在os上 执行操作: 写一个文件,文件中按照python规则写 ...

  3. 微信小程序前端开发踩坑(一)

    之前由于不了解微信小程序的整个的运行开发机制,走了很多的弯路,脑子灵光的可能不会遇到,这个主题系列的帖子希望可以帮助到像我一样理解能力慢的孩子. 不论是开发微信小程序还是说学习任何一门编程语言,最重要 ...

  4. KeyguardSliceView.java

    /* * Copyright (C) 2017 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  5. vue—data中变量和字符串拼接

    #变量和字符串的拼接# 写项目中,遇到了这样的一个问题:怎样在一个div里面显示两个data中的数据?我的问题描述清楚了吗?... 看图吧:   这是用户最初的需求~  这是用户后来的需求,嗯……就是 ...

  6. WEB测试专题之测试分类

    虽然说是一个功能测试就概括了,但是其实这里面还别有洞天,大概区分为下面几个小类别:WEB测试专题之web测试分类一(1)链接测试链接是Web应用系统的一个主要特征,它是在页面之间切换和指导用户去一些不 ...

  7. Array.sort()

    sort() : 是对数组的元素进行排序,并返回一个数组.默认排序方式是根据字符串的Unicode码表的码点. 由于取决于具体实现,所以无法保证它的时间和空间复杂度. arr.sort(compare ...

  8. Codeforces 977E:Cyclic Components(并查集)

    题意 给出nnn个顶点和mmm条边,求这个图中环的个数 思路 利用并查集的性质,环上的顶点都在同一个集合中 在输入的时候记录下来每个顶点的度数,查找两个点相连,且度数均为222的点,如果这两个点的父节 ...

  9. Java基础知识学习思维导图

  10. CentOS7虚拟机克隆,且成功互ping

    第一步:克隆 https://blog.csdn.net/mijichui2153/article/details/80918285 打开VMware,确认已经完成安装配置的CentOS7虚拟机在关闭 ...