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. php计算上个月是几月份

    PHP计算上个月的时间, $date = date("Y-m-d"); $arr = explode('-',$date); foreach ($arr as $key=>$ ...

  2. Hadoop(二)CentOS7.5搭建Hadoop2.7.6完全分布式集群

    一 完全分布式集群(单点) Hadoop官方地址:http://hadoop.apache.org/ 1  准备3台客户机 1.1防火墙,静态IP,主机名 关闭防火墙,设置静态IP,主机名此处略,参考 ...

  3. mysql5.6升级为mysql5.7部署jboss/wildfly应用项目

    一.部署mysql5.7二进制版 解压tar -xvf mv mysql-5.7  /usr/local/mysql5.7  或者其他文件夹 cd  /usr/local/mysql.57 usera ...

  4. 《Act with Prudence》读后感

    <97 Things Every Should Know>中第一个编程方面的建议 文章链接:行事谨慎 很赞同文章中的观点,在做项目中是要谨慎行事和考虑后果.一直在项目前期考虑不够周到,以至 ...

  5. Oracle笔记之——常用的函数及脚本

    一.oracle 常用的函数及关键字 1.集合操作 1)minus 差集 2)intersect 交集 3)UNION 并集,会去重 4)UNION ALL 并集,不去重2.事物 1)COMMIT ( ...

  6. Go学习笔记02

    前言 上篇内容,介绍了如何在不同的系统上安装 Go 开发环境和部分参数的配置,也简单介绍了 package 的概念.导入方式和我对包的初始化过程的理解,关于初始化顺序的理解,可能有错误,后期会有修改, ...

  7. 并查集(模板&典型例题整理)

    参考:https://blog.csdn.net/oliver233/article/details/70162173 带路径压缩模板: #include<stdio.h> ]; int ...

  8. 动手打造轻量web服务器(二)路由

    tomcat启动慢?自己动手打造轻量web服务器(一) 上篇讲了怎么做一个最简单的web服务器,这篇就是在上篇加上URL路由功能(什么是路由?) 首先,根据http获得请求行 val scanner ...

  9. 20155213 2016-2017-2《Java程序设计》课程总结

    20155213 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我所期望的师生关系 预备作业2:C语言的学习的回忆 预备作业3:小议linux 第一周作 ...

  10. 20155230 《Java程序设计》实验一(Java开发环境的熟悉) 实验报告

    练习题: 凯撒密码: import java.util.Scanner; import java.io.*; public class exp1 { public static void main(S ...