题目描述

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

说明:
尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。

解题思路

典型的利用回溯法解题。首先构造一个map,令每个数字对应各个英文字母,然后对于给出的字符串进行递归映射:

  • 对于每个数字字符,分别添加其对应的字符到结果字符串中,并向后递归
  • 若遍历到了字符串结尾,则说明各数字映射完毕,将当前字符串加入到结果集合中

代码

 class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
if(digits.empty())
return res;
map<char, string> numToStr;
numToStr['']="abc";
numToStr['']="def";
numToStr['']="ghi";
numToStr['']="jkl";
numToStr['']="mno";
numToStr['']="pqrs";
numToStr['']="tuv";
numToStr['']="wxyz";
find(digits, , "", res, numToStr);
return res;
}
void find(string digits, int idx, string s, vector<string> &res, map<char, string> numToStr){
if(idx == digits.size())
res.push_back(s);
else
for(int i = ; i < numToStr[digits[idx]].size(); i++)
find(digits, idx+, s+numToStr[digits[idx]][i], res, numToStr);
}
};

LeetCode 17. 电话号码的字母组合(Letter Combinations of a Phone Number)的更多相关文章

  1. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  2. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

  3. 电话号码的字母组合 · Letter Combinations of a Phone Number

    [抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...

  4. Java实现 LeetCode 17 电话号码的字母组合

    17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  5. LeetCode第[17]题(Java):Letter Combinations of a Phone Number

    题目:最长公共前缀 难度:EASY 题目内容: Given a string containing digits from 2-9 inclusive, return all possible let ...

  6. 【Leetcode】【Medium】Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. 【leetcode刷题笔记】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. 电话号码的字母组合(回溯)

    题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[& ...

  9. [LeetCode] 17. 电话号码的字母组合 ☆☆☆(回溯) ###

    描述 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:[&q ...

随机推荐

  1. Express multer 文件上传

    npm multer 文件上传 Express app 范本就不写了,仅记录一下上传部分的代码. const fs = require('fs'); const express = require(' ...

  2. 互联网安全架构之常见的Web攻击手段及解决办法

    一.Web 安全常见攻击手段 XSS(跨站脚本攻击) SQL 注入 CSRF(跨站请求伪造) 上传漏洞 DDoS(分布式拒绝服务攻击)等 二.攻击手段原理及解决方案 1.XSS攻击 原理:XSS 攻击 ...

  3. window.location.href 与 window.location.href 的区别

  4. Angular2 父子组件通信方式

    https://www.jb51.net/article/133868.htm 这次给大家带来Angular2 父子组件通信方式,使用Angular2 父子组件通信方式的注意事项有哪些,下面就是实战案 ...

  5. @AutoConfigureBefore

    @AutoConfigureBefore(Xxx.class)此注解用在类名上,标识在加载Xxx类前加载该配置类

  6. k8sReplicaSet控制器

    一.ReplicaSet概述 简称RS,是pod控制器类型的一种实现,用于确保由其管控的pod对象副本数在任一时刻都能精确满足期望的数量.ReplicaSet控制器资源启动后会查找集群中匹配其标签选择 ...

  7. Notepad++设置运行快捷键

    python: 先按F5,之后将下面的命令保存,再设置快捷键. cmd /k c:\python27\python "$(FULL_CURRENT_PATH)" & PAU ...

  8. 一个原生ajax在jetbrains开发平台的调用方法

    这段随笔的记述目的无非是,一个html页面中可能有多段js代码,所以采用外引的方法应该会好一些 function checkfiles() { var xhr = new XMLHttpRequest ...

  9. python插入mysql数据(2)

    python插入mysql数据(2) """插入操作""" import pymysql import datetime from pymy ...

  10. dede cms 怎样调用年月日

    一: 首页:([field:pubdate function='strftime("%m-%d",@me)'/])==(5-15)([field:pubdate function= ...