425-电话号码的字母组合

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

A mapping of digit to letters (just like on the telephone buttons) is given below.

注意事项

以上的答案是按照词典编撰顺序进行输出的,不过,在做本题时,你也可以任意选择你喜欢的输出顺序。

样例

给定 "23"

返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

标签

递归 回溯法 字符串处理 脸书 优步

思路

使用回溯 + 递归

code

class Solution {
public:
/*
* @param digits: A digital string
* @return: all posible letter combinations
*/
vector<string> letterCombinations(string digits) {
// write your code here
if (digits.size() <= 0) {
return vector<string>();
}
vector<string> result;
string str;
letterCombinations(digits, str, 0, result);
return result;
} void letterCombinations(string &digits, string &str, int index, vector<string> &result) {
if (index == digits.size()) {
result.push_back(str);
return;
}
string base[] = { "", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
for (int i = 0; i < base[digits[index] - '0'].size(); i++) {
str += base[digits[index] - '0'][i];
letterCombinations(digits, str, index + 1, result);
str.pop_back();
}
}
};

lintcode-425-电话号码的字母组合的更多相关文章

  1. lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

    题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...

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

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

  3. LeetCode(17):电话号码的字母组合

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

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

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

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

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

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

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

  7. Leetcode13. 罗马数字转整数Leetcode14. 最长公共前缀Leetcode15. 三数之和Leetcode16. 最接近的三数之和Leetcode17. 电话号码的字母组合

    > 简洁易懂讲清原理,讲不清你来打我~ 输入字符串,输出对应整数 ![在这里插入图片描述](https://img-blog.csdnimg.cn/63802fda72be45eba98d9e4 ...

  8. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  9. leetcode(js)算法之17电话号码的字母组合

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

  10. Java实现LeetCode17. 电话号码的字母组合

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

随机推荐

  1. MongoDB 简单上手

    MongoDB 是什么 MongoDB 是一种非关系型数据库(NoSQL). MongoDB中的术语解释 文档(document):形如 { name: "sue", <-- ...

  2. GD32F450 200M时USB不稳定

    使用GD32F450的demo修改usb驱动,发现120M和168M时正常,200M时很不稳定.怀疑USB时钟分频有问题,一查果然是,记录如下: 200M是库函数主时钟分频代码如下 static vo ...

  3. 我遇到的有关git的问题及解决方法总结

    有关git的问题 ** 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.git add ...

  4. 2017-2018-1 20155338《信息安全技术》实验二——Windows口令破解

    2017-2018-1 20155338<信息安全技术>实验二--Windows口令破解 一.试验环境 系统环境:Windows 实验工具: LC5 SuperDic 二.实验内容及要求 ...

  5. 20155339 《信息安全技术》实验二、Windows口令破解实验报告

    20155339 <信息安全技术>实验二.Windows口令破解实验报告 实验目的 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破解 系统环境 Windo ...

  6. 【转载】ID3DXSPRITE接口简单使用

    原文:ID3DXSPRITE接口简单使用 前些日子一直研究DDraw,毕竟是DirectX7的东西了,所以转手用DirectD3D9,用了Surface进行绘图,可是怎么做透明色也都是不行loadfr ...

  7. GDAL中通过GDALDriver类的Create函数实现图像的保存

    GDAL中除了读取各种类型的图像外,也可以实现对各种图像的保存操作,具体实现测试代码如下: int test_gadl_GDALDataset_write() { const char* image_ ...

  8. Yii 2.0 中事件的使用

    关于PHP的事件处理,参照 http://www.cnblogs.com/mafeifan/p/4322238.html http://www.cnblogs.com/mafeifan/p/43222 ...

  9. HBase第一章 安装 HMaster 主备

    1.集群环境 Hadoop HA 集群规划 hadoop1 cluster1 nameNode  HMaster hadoop2 cluster1 nameNodeStandby ZooKeeper ...

  10. EmitMapper自动映射工具

             在实体与DTO之间,我们一般都需要进行映射.如果手动的来进行转换,实在是太麻烦.所以就产生了很多映射工具,比如AutoMapper,EmitMapper.而经过一些对比,EmitMa ...