[LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
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.
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 电话号码的字母组合的更多相关文章
- lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合
题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 [& ...
- [LeetCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 017 Letter Combinations of a Phone Number 电话号码的字母组合
给定一个数字字符串,返回数字所有可能表示的字母组合. 输入:数字字符串 "23"输出:["ad", "ae", "af" ...
- Leetcode17.Letter Combinations of a Phone Number电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- LeetCode OJ:Letter Combinations of a Phone Number(数字字母组合)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number 电话号码组合
题意:给一个电话号码,要求返回所有在手机上按键的组合,组合必须由键盘上号码的下方的字母组成. 思路:尼玛,一直RE,题意都不说0和1怎么办.DP解决. class Solution { public: ...
随机推荐
- ICP 算法步骤
The Iterative Closest Point (ICP) is an algorithm employed to match two surface representations, suc ...
- Java Thread join() 的用法
Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...
- Codeforces Alpha Round #20 (Codeforces format) C. Dijkstra?(裸的dijkstra)
题目链接:http://codeforces.com/problemset/problem/20/C 思路:需要用优化过的dijkstra,提供两种写法. #include <iostream& ...
- 【SSH】 之 Struts2环境搭建及简单应用开发
在上一篇文章中,我们一起了解了一下struts2的工作机制原理,接下来让我们进行一下简单应用的开发 (一)配置环境 1.建立web项目 2.导入jar包 其中struts2中有很多jar包,我们不需要 ...
- Fragment详解之三——管理Fragment(1)
相关文章: 1.<Fragment详解之一--概述>2.<Fragment详解之二--基本使用方法>3.<Fragment详解之三--管理Fragment(1)>4 ...
- JavaScrip入门(3)
函数: var m2=function(){ console.log('2222'); } console.log(typeof(m2)); 输出结果:test.html:31 function js ...
- matlab坐标外围背景变白色
set(gcf,'Color',[1,1,1]) 默认图片是这样的: 出图之前使用命令,外围变白后效果如下:
- 解决js小数求和出现多位小数问题
在小数相加时,可能会产生多个小数位.如下所示: var x=1+1; //2 var x=1.20+1.11; //2.31 var x=1.56+1.76; //3.3200000000 ...
- iOS实现简书的账号识别方式(正则表达式)
通过简书iOS客户端登录,我们会看到请输入手机号或者邮箱登录,但是我们随机输入1234567的时候,便会弹出手机格式不正确,同样也会识别我们的邮箱格式,那么我们在项目中怎么实现这种判断呢? 0E471 ...
- Zookeeper 分布式环境搭建
一.前期环境 安装概览 IP Host Name Software 192.168.23.128 ae01 JDK 1.7 192.168.23.129 ae02 JDK 1. ...