[LeetCode]Letter Combinations of a Phone Number题解
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.
![]()
Input:Digit string “23”
Output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.
简单讲讲这道题的思路吧。一开始想到的方法是,由给定的digits字符串的每个字符,确定对应的若干个(3或4)字符,然后通过递归,从底层开始,每次将这一层的字符和vector中的字符串组合一下,再返回给上一层。
思路是挺清晰也挺简单的,不过c++实现过程中也遇到了一点困难。
- char转换成string的问题。递归最底层要实现这个,“”+ ch并不能做到转string,最后是用一个空的string,push_back字符得到想要的结果。
- digits的字符对应若干个字符的问题。由于太久没有写这些东西,脑子不是很清晰,一开始也写错了,最终跟这个相关的代码也是有些乱的。
第二种是非递归的方法,每次把数字对应的3或4个字符添加到结果集合中。
最后有给出python实现的代码,非递归,非常简单。
class Solution {
public:
vector<string> letterCombinations(string digits) {
return combine(digits,0);
}
std::vector<string> combine(string digits,int len){
vector<string> re,temp;
//threeOrFour:这个字符对应着几个字符;
//ext:为了7(对应4个字符)以后的数字对应字符都+了1而定义的int,值是0或1
int threeOrFour,ext;
if(digits[len] == '7' || digits[len] == '9'){
threeOrFour = 4;
}else{
threeOrFour = 3;
}
if(digits[len] > '7'){
ext = 1;
}else{
ext = 0;
}
//ch是该数字对应的第一个字符
char ch = (digits[len] - 48) * 3 + 91 + ext;
string empty = "";
if ( len < digits.size()){
temp = combine(digits,len+1);
}
//递归的最底层
if (len == digits.size() - 1){
string t;
for(int i = 0; i < threeOrFour; i++){
t = empty;
t.push_back(ch+i);
re.push_back(t);
}
return re;
}
else{
for (int i = 0; i < temp.size(); ++i){
for(int j = 0; j < threeOrFour; j++){
string str = empty;
str.push_back(ch + j);
re.push_back((str+temp[i]));
}
}
return re;
}
}
};
下面是修改之后,更加简洁的版本:
class Solution {
private:
string letters[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
vector<string> letterCombinations(string digits) {
return Mycombine(digits,0);
}
vector<string> Mycombine(string digits,int len){
std::vector<string> re, temp;
temp.push_back("");
if(digits.empty()) return re;
if(len < digits.size() - 1){
temp = Mycombine(digits,len+1);
}
string get = letters[toInt(digits[len])];
for (int i = 0; i < temp.size(); ++i){
for (int j = 0; j < get.size(); ++j){
string put ="";
put.push_back(get[j]);
put += temp[i];
re.push_back(put);
}
}
return re;
}
int toInt(char ch){
return ch - 48;
}
};
迭代方法:
class Solution {
private:
string letters[10] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
vector<string> letterCombinations(string digits) {
std::vector<string> re,temp;
if(digits.empty()) return re;
re.push_back("");
//temp.push_back("");
for(int i = 0; i < digits.size(); i++){
string get = letters[toInt(digits[i])];
for(int j = 0; j < re.size(); j++){
string t = re[j];
for(int k = 0; k < get.size(); k++){
string str = t;
str.push_back(get[k]);
temp.push_back(str);
}
}
re = temp;
temp.clear();
}
return re;
}
int toInt(char ch){
return ch - 48;
}
};
事实上,用python的话非常好实现,而且逻辑很清晰:
class Solution:
def letterCombinations(self, digits):
"""
:type digits: str
:rtype: List[str]
"""
if len(digits) == 0:
return []
re = ['']
chars = ['','','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']
m = {i:[ch for ch in chars[i]] for i in range(0,10)}
data = [int(digits[i]) for i in range(len(digits)) ]
for i in data:
temp = []
for s in re:
for j in m[i]:
temp.append(s + j)
print(j)
re = temp
return re
[LeetCode]Letter Combinations of a Phone Number题解的更多相关文章
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode] 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
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode] 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(bfs)
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- LeetCode Letter Combinations of a Phone Number (DFS)
题意 Given a digit string, return all possible letter combinations that the number could represent. A ...
- [LeetCode] 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: ...
- leetcode Letter Combinations of a Phone Number python
class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...
随机推荐
- javascript解决getElementById()的bug以及getElementsByClassName的兼容性写法
<a name="target" href="#">链接</a> <p id="target">文字说明 ...
- PDF转HTML的方法。
上个项目客户提出了一个需求,要求把PDF格式的文件转化为HTML格式. 上网查了一下,要么使用软件处理,要么是HTML格式转化为PDF.因为涉及到图文识别问题,所以说仅仅依靠前端不能实现.在网上查了几 ...
- 半年的iOS代码生活
半年的iOS代码生活 在高考大军中拼杀过,也在大学校园中荒芜过,曾经低迷消沉,也常满怀壮志…… 但是最多的还是被称为小伙子以及自称为iOS工程师!博主就是这种喜闻乐见的这类人,实习一年后在2015年的 ...
- kao shi
1 #include "date.h" #include "utils.h" #include <iostream> using std::cout ...
- session_destroy()和session_unset()的理解
session_destroy 是注销所有的session变量,并且结束session会话目前是删除当前用户对应的session文件以及释放session id值 ,但是但是 内存中的$_SESSIO ...
- Unity3D实现随机播放背景音频
1.先在第一人称下新建空白物体,命名“audio” 2.在audio中加入Audio Source 3.在第一人称组件里添加Audio Liistener和Audio脚本 4.脚本中添加代码 usin ...
- CSS外边距合并&块格式上下文
前言问题Margin Collapsing 外边距合并Block Formatting Context 块格式化上下文解决方案参考 前言 之前在前端开发的过程中,都没有遇到外边距合并的问题(其实是因为 ...
- Mac下关闭Sublime Text 3的更新检查
操作如下: 注意:update_check的属性前后都要有一个逗号. , "update_check":false, 然后还需要一步,就是注册破解,在[Help]->[Ent ...
- css中字体单位px,pt,em,百分比之间的区别和用法
px 即像素,一般国内网站使用较多,默认大小是16px; pt 印刷行业常用单位 em 相对单位,相对父元素属性的单位 ,一般用于移动端布局 rem 结合相对定位和绝对定位的优势,相对根元素htm ...
- 029-FastDFSClient工具栏模板
模板一: package cn.e3mall.common.utils; import org.csource.common.NameValuePair; import org.csource.fas ...