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题解的更多相关文章

  1. LeetCode: Letter Combinations of a Phone Number 解题报告

    Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...

  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——Letter Combinations of a Phone Number

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

  4. [LeetCode] Letter Combinations of a Phone Number

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

  5. [LeetCode] Letter Combinations of a Phone Number(bfs)

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

  6. LeetCode Letter Combinations of a Phone Number (DFS)

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

  7. [LeetCode] Letter Combinations of a Phone Number 回溯

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

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

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

  9. leetcode Letter Combinations of a Phone Number python

    class Solution(object): def letterCombinations(self, digits): """ :type digits: str : ...

随机推荐

  1. Windows 计划任务之消息提醒

    Windows 计划任务之消息提醒 你肯定也有这种需求.想做一个计划任务,却发现老式消息提醒已经被微软禁止了. 或者就是很单纯的希望给系统弹出一个消息框而并非CMD的echo命令. so...how ...

  2. C++与C的区别二

    1. new,delete的局部重载: #include <iostream> using namespace std; ; class myclass { public: myclass ...

  3. Centos7.4下安装Nginx

    一.下载Nginx Nginx下载地址:http://nginx.org/en/download.html Nginx是C语言开发的,建议在Linux上运行.由于Nginx的一些模块依赖一些lib,所 ...

  4. CSS01--概述与选择器

    CSS:Cascading Style Sheets,层叠样式表.我们之前已经说过,HTML解决的是网页内容(结构)的问题,而CSS立足于网页的表现方面的问题,则样式定义如何显示HTML标签,js负责 ...

  5. 生成allure测试报告之后,服务器端口无法访问查看生成的report,可能是这样引起的。

    1. 检查防火墙 2. 如果机器有安装ADsafe,请关闭adsafe后重试

  6. [JZOJ6075]【GDOI2019模拟2019.3.20】桥【DP】【线段树】

    Description N,M<=100000,S,T<=1e9 Solution 首先可以感受一下,我们把街道看成一行,那么只有给出的2n个点的纵坐标是有用的,于是我们可以将坐标离散化至 ...

  7. 九省联考 2018 Day 1 复现

    前言 今年省选还有 15 天.每天针对性刷题学知识点有点枯燥,想到真题还没刷,就对着 pdf 做了一遍. A. 一双木棋 去年省选得了 25,应该是 \(n=2,m=2\) 的贪心和 \(m=1\) ...

  8. curl 命令大全

    post json curl -H "Content-Type: application/json" -X POST --data '{"userID":100 ...

  9. Neo4j安装&入门&一些优缺点(转)

    本篇将介绍Neo4j的安装,入门,和自己使用了一段时间后发现的优点缺点,争取简洁和实用. 如果你是第一次接触Neo4j,并且之前也都没接触过类似的Graph Database的话,建议先浏览一下我之前 ...

  10. VS2010一调试就卡死的问题解决方案 (转)

    前几天我的vs2010突然不能调试了.后来找了很多的解决方案都不能解决问题. 1. 删除所有的bin, debug 2. 重建工程. 3. 重新安装Silverlight的开发包. 还是不行. 最后是 ...