题目:

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.

代码:

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ret;
if ( digits.empty() ) return ret;
map<int, string> digitLetters;
digitLetters[] = "";
digitLetters[] = "";
digitLetters[] = "abc";
digitLetters[] = "def";
digitLetters[] = "ghi";
digitLetters[] = "jkl";
digitLetters[] = "mno";
digitLetters[] = "pqrs";
digitLetters[] = "tuv";
digitLetters[] = "wxyz";
vector<int> letterBegin(,);
int index = ;
string tmp;
Solution::combine(ret, tmp, index, digits, digitLetters, letterBegin);
return ret;
}
static void combine(
vector<string>& ret,
string& tmp,
int index,
string& digits,
map<int,string>& digitLetters,
vector<int>& letterBegin)
{
if (index==digits.size())
{
ret.push_back(tmp);
return;
}
int curr = digits[index]-'';
string letters = digitLetters[curr];
for ( int i = ; i < letters.size(); ++i )
{
letterBegin[curr] = i;
tmp.push_back(letters[i]);
Solution::combine(ret, tmp, index+, digits, digitLetters, letterBegin);
tmp.erase(tmp.end()-);
}
}
};

tips:

上述的代码是AC的。思路也就是常规dfs的思路:

1. 终止条件是层级到了digits的长度

2. 每一层递归相当于根据该位置的数字选一个字母

但是,个人感觉这道题的题干要求没说清楚;既然是letter combinations,而不是letter permutation,那么“ac”和“ca”就应该算一个combination,但是OJ之后发现默认“ac”和“ca”算两个。加入“ac”和“ca”算一个,有没有解法呢?代码如下:

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ret;
if ( digits.empty() ) return ret;
map<int, string> digitLetters;
digitLetters[] = "";
digitLetters[] = "";
digitLetters[] = "abc";
digitLetters[] = "def";
digitLetters[] = "ghi";
digitLetters[] = "jkl";
digitLetters[] = "mno";
digitLetters[] = "pqrs";
digitLetters[] = "tuv";
digitLetters[] = "wxyz";
vector<int> letterBegin(,);
int index = ;
string tmp;
Solution::combine(ret, tmp, index, digits, digitLetters, letterBegin);
return ret;
}
static void combine(
vector<string>& ret,
string& tmp,
int index,
string& digits,
map<int,string>& digitLetters,
vector<int>& letterBegin)
{
if (index==digits.size())
{
ret.push_back(tmp);
return;
}
int curr = digits[index]-'';
string letters = digitLetters[curr];
for ( int i = letterBegin[curr]; i < letters.size(); ++i )
{
letterBegin[curr] = i;
tmp.push_back(letters[i]);
Solution::combine(ret, tmp, index+, digits, digitLetters, letterBegin);
tmp.erase(tmp.end()-);
}
}
};

tips:

多维护一个letterBegin的数组,标记“当前的组合中,某个数字对应的字母序列中应该从第几个字母开始取”。

如果输入是“22”,那么给出的解集就是:

aa

ab

ac

bb

bc

cc

从这个例子可以看出来,算法的原理就是维护某一个数字对应字母序列:如果数字重复出现,那么对应的字母要保证字典序递增,这样就不会用重复的。

可惜题目并不是这么要求的。

===============================================

既然题目要求简单了,则再追求一个迭代的解法。AC的代码如下:

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ret;
if (digits.empty()) return ret;
ret.push_back("");
map<int, string> digitLetters;
digitLetters[] = "abc";
digitLetters[] = "def";
digitLetters[] = "ghi";
digitLetters[] = "jkl";
digitLetters[] = "mno";
digitLetters[] = "pqrs";
digitLetters[] = "tuv";
digitLetters[] = "wxyz";
for ( size_t i = ; i < digits.size(); ++i )
{
int curr = digits[i]-'';
string letters = digitLetters.find(curr)==digitLetters.end() ? "" : digitLetters[curr];
vector<string> tmp = ret;
ret.clear();
for ( size_t j = ; j < tmp.size(); ++j )
{
for ( size_t k = ; k < letters.size(); ++k )
{
string ori = tmp[j];
ori += letters[k];
ret.push_back(ori);
}
}
}
return ret;
}
};

完毕。

======================================

第二次过这道题,用dfs过的。注意如果原来的digits==""返回的也是空。

class Solution {
public:
vector<string> letterCombinations(string digits)
{
map<char, string> digit_letters;
digit_letters[''] = "abc";
digit_letters[''] = "def";
digit_letters[''] = "ghi";
digit_letters[''] = "jkl";
digit_letters[''] = "mno";
digit_letters[''] = "pqrs";
digit_letters[''] = "tuv";
digit_letters[''] = "wxyz";
vector<string> ret;
if ( digits=="" ) return ret;
vector<char> tmp;
Solution::dfs(ret, digits, tmp, digit_letters);
return ret;
}
static void dfs(
vector<string>& ret,
string digits,
vector<char>& tmp,
map<char,string>& digit_letters)
{
if ( tmp.size()==digits.size() )
{
ret.push_back(string(tmp.begin(),tmp.end()));
return;
}
int index = tmp.size();
for ( int i=; i<digit_letters[digits[index]].size(); ++i )
{
tmp.push_back(digit_letters[digits[index]][i]);
Solution::dfs(ret, digits, tmp, digit_letters);
tmp.pop_back();
}
}
};

【Letter Combinations of a Phone Number】cpp的更多相关文章

  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之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

    [Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...

  3. [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合

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

  4. 69. Letter Combinations of a Phone Number

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

  5. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  6. Letter Combinations of a Phone Number:深度优先和广度优先两种解法

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

  7. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...

  8. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. Letter Combinations of a Phone Number - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...

随机推荐

  1. JavaScript获取URL参数公共方法

    写一个JavaScript获取URL参数的通用方法,可以把它放到常用方法的JS文件中调用,直接上代码例子,新手可以学习一下! <!DOCTYPE html> <html lang=& ...

  2. python字符串及字符串操作

    字符串介绍 1.字符串在内存中的存储: 2.字符串相加: 3.字符串的格式化: In [1]: a = 100 In [2]: a Out[2]: 100 #100<255,在堆内存下占用了一个 ...

  3. Piwik-2.16.1 (OpenLogic CentOS7.2)

    平台: CentOS 类型: 虚拟机镜像 软件包: centos7.2 piwik devops log analysis monitoring open-source 服务优惠价: 按服务商许可协议 ...

  4. 360、IE等浏览器对bootstrap的影响

    笔者开发的web程序部署上线后发现,bootstrap的菜单不显示,开发时候用chrome没有发现问题,在360浏览器上跑,发现360默认的是兼容模式,切换到极速模式就能够显示菜单了. 但是这样的用户 ...

  5. c++ STL deque容器成员函数

    deque是双向队列,即可以在头部插入删除,也可以在尾部插入删除.内部并不连续,这一点和vector并不一样.可能第1个元素和第2个元素的地址是不连在一起的.在使用时用it迭代器会安全一点. 这是c+ ...

  6. 标准IO ——将A文件fpd第3个字节之后的内容复制到文件fps

    /* *使用标准IO ——将A文件fpd第3个字节之后的内容复制到文件fps 流程: 1.创建两个流,链接目标文件和源文件 2.输入流的基准点偏移四个单位然后输入缓冲区 3.输出流读取缓冲区数据送入文 ...

  7. POJ-2226 Muddy Fields---二分图匹配+巧妙构图

    题目链接: https://vjudge.net/problem/POJ-2226 题目大意: 用宽度为1长度不限的木板将水洼‘*’盖住而不盖住草‘.' Sample Input 4 4 *.*. . ...

  8. 自动释放池的前世今生 ---- 深入解析 autoreleasepool

    http://draveness.me/autoreleasepool.html 关注仓库,及时获得更新:iOS-Source-Code-Analyze Follow: Draveness · Git ...

  9. python_29_三级菜单2

    menu={'河北':{ '石家庄':{ '平山':{ '县城':{}, '寨北':{}, '苏家庄':{}, }, '灵寿':{}, '鹿泉':{} }, }, '北京':{ '昌平':{ '沙河' ...

  10. CUDA高性能编程中文实战11章例子中多设备的例子编译提示问题

    提示的问题如下: error : argument of type "void *(*)(void *)" is incompatible with parameter of ty ...