[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: ...
随机推荐
- Ubuntu14.04LTS系统输入法的安装
由于安装的时候选择的是英文版,所以一进入系统问题就来了:无法输入中文. 我记得自己直接选的输入法是pinyin那个 在网上看到别人到blog,直接转过来吧,只为自己收藏下,如有需要请联系原作者. 转载 ...
- Error:“应用程序无法正常启动(0xc000007b)。请单击“确定”关闭应用程序。”
我的电脑是 win7 64bit,用 VS2012 跑网上下载的程序,Realease | x64 模式下出现该错误. 问题出在 freeglut.dll 是 32bit 下的 dll,需要换成 64 ...
- poj 1816 (Trie + dfs)
题目链接:http://poj.org/problem?id=1816 思路:建好一颗Trie树,由于给定的模式串可能会重复,在原来定义的结构体中需要增加一个vector用来记录那些以该节点为结尾的字 ...
- CmRegisterCallback使用方法
部分代码 #include "my_sys_fun.h"#ifdef __cplusplusextern "C"{#endif //驱动加载函数 NTSTATU ...
- 分布式文件系统FastDFS设计原理(转)
FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server).存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题 ...
- javascript同名变量
我写个流程:在流程之前,必须写一下标识符是啥. 一句话,就是variable object的属性.而这个对象会被不同执行环境来决定. 比如全局环境下的variable object 就是 global ...
- cf 106C
题目链接:http://vjudge.net/contest/139376#problem/E 题意看注释就能懂了,求能获得的最大价值. 代码: #include<iostream> #i ...
- 【Clr in c#】泛型
使用泛型的好处是“代码重用”,极大的提高了开发效率,泛型为开发者提供了以下优势: 1,源代码保护 算法的源代码不需要提供给使用泛型算法的开发人员,使用c++模板的泛型技术需要提供.(目前c++模板的 ...
- JS(event事件)
常用的event事件: 属性 此事件发生在何时... onabort 图像的加载被中断. onblur 元素失去焦点. onchange 域的内容被改变. onclick 当用户点击某个对象时调用的事 ...
- 找规律 Codeforces Round #290 (Div. 2) A. Fox And Snake
题目传送门 /* 水题 找规律输出 */ #include <cstdio> #include <iostream> #include <cstring> #inc ...