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. 树莓派3B+简单入门

    刚刚入手一个树莓派3B+,树莓派板子.3.5寸电阻触摸屏.16G内存卡.外壳电源等一系列配件一共花了360大洋,这东西真不便宜.这里介绍一下系统安装.3.5寸屏幕安装.VNC远程屏幕. 先给大家看一下 ...

  2. python3 str各个功能记录

    capitalize() 将字符串的第一个字符转换为大写 center(width, fillchar) 返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格. c ...

  3. linux查看文件命令tail的使用

    一.介绍 linux tail命令用途是依照要求将指定的文件的最后部分输出到终端中,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,tail会自己主动刷新,确保你看到最新的档案内 ...

  4. Dbshop v1.3任意用户密码重置漏洞

    0x00 前言 年也过的差不多了,各自也都回到岗位忙碌起来了,新的一年祝大家诸事顺利,洞洞高危!好了进入正题 0x01 漏洞简介 本次主要写个简单的逻辑漏洞!然后抛个Message 内置高危. 1.找 ...

  5. python 模块路径查找 及 添加

    **** python 模块路径查找: 通过模块的__file__属性来确定: **** 模块路径添加: 方法一:函数添加1 import sys2 查看sys.path3 添加sys.path.ap ...

  6. 20155206 《Java程序设计》实验三实验报告

    20155206 <Java程序设计>实验三实验报告 实验内容 Java敏捷开发与XP实践 实验内容 XP基础 XP核心实践 相关工具 实验步骤 提交一: 提交二: 提交三: 提交四:

  7. 20155229 2016-2017-2 《Java程序设计》第三周学习总结

    20155229 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章 BigDecimal提供有plus().substract().multiply() ...

  8. mycp 补交作业

    老师好:我昨天做完时已经是11点多了,错过了提交时间,希望用此篇博客弥补一下我的过失. import java.io.; import java.lang.; import java.util.Sca ...

  9. 20155308 实验四 Android开发基础

    20155308 实验四 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android.组件.布局管理器的使用: 掌握Android中 ...

  10. 20145209刘一阳《网络对抗》Exp8 Web基础

    20145209刘一阳<网络对抗>Exp8 Web基础 基础问题回答 1.什么是表单? 表单是一个包含表单元素的区域,表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等 ...