一.题目链接:

  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. Git从入门到差不多会用

    工作以后最先接触到的新东西可能就包括版本控制工具了,对Git的感觉是又敬又畏,敬是因为最初的时候都是跟着同事照猫画虎地通过开发软件图形化操作,大家都不太懂,也不知道这东西有多深奥:畏就是因为有过几次惨 ...

  2. 理解JavaScript中的属性描述符

    我们把描述JavaScript中定义内部特性的属性叫做属性描述符 分为两大类:数据描述符和存取描述符 数据描述符是一个拥有可写或不可写的属性(Writable); 存取描述符不包含数据值,是一组拥有g ...

  3. UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  请通过右侧公告中的“联系邮 ...

  4. Linux系统安装与初用

    1.结合实验尝试,并查阅资料,总结在实验准备中提出的7个问题. (1).Linux的发行版本.内核版本:二者区别与联系. 核心版本主要是Linux的内核,发行版本是各个公司推出的版本,他们与核心版本是 ...

  5. React native 中 SectionList用法

    一.代码 import React, { Component } from 'react'; import { AppRegistry, View, Text, SectionList, } from ...

  6. How to Animate UILabel textColor Properties

    How to Animate UILabel Properties UILabel properties cannot be easy animated due to some various rea ...

  7. 分享一个好用的tmux配置文件

    tmux众所周知,不过多介绍,友好的tmux配置,让人用起来很舒服,分享一个tmux配置文件 # ------ general ------------------------------------ ...

  8. Visual C++ 6.0中if..else..的简单用法和基本格式

    # include <stdio.h> int main (void) { float score; printf("请输入您的考试成绩:"); scanf(" ...

  9. org.jsoup.Jsoup找不到jar包问题解决思路

    今天在idea中导入项目,出现了这样的问题 通过idea的自带的导包功能,却提示找不到这个东西.于是就去maven仓库搜索这个咚咚 <!-- https://mvnrepository.com/ ...

  10. 【Effective Java读书笔记】创建和销毁对象(一):考虑使用静态工厂方法代替构造器

    类可以提供一个静态方法,返回类的一个静态实例,如Boolean包装类的一个获取实例的静态方法 public static Boolean valueOf(boolean b) { return (b ...