Given a digit string, 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.

Notice

Although the above answer is in lexicographical order, your answer could be in any order you want.

Have you met this question in a real interview?

Yes
Example

Given "23"

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

 

LeetCode上的原题,请参见我之前的博客Letter Combinations of a Phone Number

解法一:

class Solution {
public:
/**
* @param digits A digital string
* @return all posible letter combinations
*/
vector<string> letterCombinations(string& digits) {
if (digits.empty()) return {};
vector<string> res;
vector<string> v{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
helper(digits, v, , "", res);
return res;
}
void helper(string& digits, vector<string>& v, int level, string out, vector<string>& res) {
if (level == digits.size()) {
res.push_back(out);
return;
}
string t = v[digits[level] - ''];
for (int i = ; i < t.size(); ++i) {
out.push_back(t[i]);
helper(digits, v, level + , out, res);
out.pop_back();
}
}
};

解法二:

class Solution {
public:
/**
* @param digits A digital string
* @return all posible letter combinations
*/
vector<string> letterCombinations(string& digits) {
if (digits.empty()) return {};
vector<string> res{""};
vector<string> v{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for (int i = ; i < digits.size(); ++i) {
string str = v[digits[i] - ''];
int n = res.size();
for (int j = ; j < n; ++j) {
string t = res.front();
res.erase(res.begin());
for (int k = ; k < str.size(); ++k) {
res.push_back(t + str[k]);
}
}
}
return res;
}
};

[LintCode] Letter Combinations of a Phone Number 电话号码的字母组合的更多相关文章

  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. Letter Combinations of a Phone Number 电话号码的字母组合

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

  4. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

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

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

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

    给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...

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

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

  8. LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)

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

  9. LeetCode Letter Combinations of a Phone Number 电话号码组合

    题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...

随机推荐

  1. [CentOS]安装软件:/lib/ld-linux.so.2: bad ELF interpreter解决

    转自:http://blog.csdn.net/wanglei2258/article/details/24961233 [CentOS]安装软件:/lib/ld-linux.so.2: bad EL ...

  2. 转一篇dudu大人的文章:程序员,用NuGet管理好你的包包

    每个女人都有很多包包:其实男人也有,但只有会写程序的男人才有 —— 代码世界中的大“包”小“包”.这些大包小包,有花钱买的,有从开源市场淘的,也有自己或同事亲手制作的. 包包有个特点:容易坏,更新快, ...

  3. c++ 左值 和 右值

    什么是lvalue, 什么是rvalue? lvalue: 具有存储性质的对象,即lvalue对象,是指要实际占用内存空间.有内存地址的那些实体对象,例如:变量(variables).函数.函数指针等 ...

  4. 【SSH】 之 Struts

    (一)什么是Struts,Struts是什么? Struts是学习轻量级J2EE框架所必须要了解的一个框架,也是我们当前最最流行的三大框架(SSH——Struts,Spring,Hibernate)之 ...

  5. SU Demos-06Selecting Traces

    不足之处,欢迎批评指正 共3个脚本,先看readme 第1个脚本 运行结果 第2个脚本 运行结果 第3个脚本 运行结果

  6. TestNg依赖详解(三)------灵活的文件配置依赖

    配置型的依赖测试,让依赖测试不局限于测试代码中,在XML文件中进行灵活的依赖配置 原创文章,版权所有,允许转载,标明出处:http://blog.csdn.net/wanghantong Javaco ...

  7. virtualtree 的使用(Delphi)

    VirtualTreeview的强大,毋庸置疑,不过,你能给演示演示,也不错,就是刚下来,只有一个可执行程序,感觉像病毒. 最近比较忙,没有上网,现在把我研究的结果和大家通报下,方便新手学习,避免走弯 ...

  8. html标题上加上小图标

    一般网站标题上都会有小图标,后面跟上文字,实现代码如下: <link rel = "Shortcut Icon" href="images/nav_logo.ico ...

  9. Android定位方式和测试方法

    Android常用的三种定位方式有:基于GPS定位.基于基站地位.基于wifi定位. 1.基于GPS定位: GPS定位需要GPS模块(硬件)的支持,没有GPS模块是无法进行GPS定位的. GPS定位最 ...

  10. C# 词法分析器(六)构造词法分析器

    系列导航 (一)词法分析介绍 (二)输入缓冲和代码定位 (三)正则表达式 (四)构造 NFA (五)转换 DFA (六)构造词法分析器 (七)总结 现在最核心的 DFA 已经成功构造出来了,最后一步就 ...