[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 : ...
随机推荐
- python中多线程,多进程,多协程概念及编程上的应用
1, 多线程 线程是进程的一个实体,是CPU进行调度的最小单位,他是比进程更小能独立运行的基本单位. 线程基本不拥有系统资源,只占用一点运行中的资源(如程序计数器,一组寄存器和栈),但是它可以与同属于 ...
- P5282 【模板】快速阶乘算法(多项式运算+拉格朗日插值+倍增)
题面 传送门 前置芝士 优化后的\(MTT\)(四次\(FFT\)) 题解 这里有多点求值的做法然而被\(shadowice\)巨巨吊起来打了一顿,所以来学一下倍增 成功同时拿到本题最优解和最劣解-- ...
- php性能优化二(PHP配置php.ini)
PHP优化对于PHP的优化主要是对php.ini中的相关主要参数进行合理调整和设置,以下我们就来看看php.ini中的一些对性能影响较大的参数应该如何设置. # vi /etc/PHP.ini (1) ...
- html基础整理(01居中 盒子问题)
01 文字居中 将一段文字置于容器的水平中点,只要设置text-align属性即可: text-align:center; 02 容器水平居中 先为该容器设置一个明确宽度,然后将margin的水平 ...
- spring与shiro配置详解
1.加入shiro相关依赖的jar包 pom.xml部分内容如下: <dependency> <groupId>org.apache.shiro</groupId> ...
- Java 访问权限控制:你真的了解 protected 关键字吗?
摘要: 对于类的成员而言,其能否被其他类所访问,取决于该成员的修饰词:而对于一个类而言,其能否被其他类所访问,也取决于该类的修饰词.在Java中,类成员访问权限修饰词有四类:private,无(包访问 ...
- error occurred during initialization of vm
虚拟机无法进行如下分配 : -Xmx2048m -XX:MaxPermSize=512m 原因是我的老爷机总共内存只有3G: settings - > 搜索VM ->找到Compiler ...
- 论文分享NO.1(by_xiaojian)
论文分享第一期-2019.03.14: 1. Non-local Neural Networks 2018 CVPR的论文 2. Self-Attention Generative Adversar ...
- dubbo集群容错之LoadBalance
原文地址:Dubbo 源码分析 - 集群容错之 LoadBalance dubbo 提供了4种负载均衡实现,分别是基于权重随机算法的 RandomLoadBalance.基于最少活跃调用数算法的 Le ...
- Mac拷贝/复制文件夹路径快捷键
快捷键:Option+Command+C 显示路径在Finder: defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES ...