题目

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”].

分析

本题目要求求出多个字符串按字母全排得到的字符串集合。

我们知道求两个字符串的字母全排是简单的,只需要两次遍历,按照字母组合即可。

那么如何求多个字符串的全排呢?尤其是字符串的个数还是不固定的,对此(字符串大于等于3个时),我采用的解决办法是,先求出前两个的全排集合v,然后,逐次将集合中的字符串与第三个字符串的字母连接,得到新的集合。

详细思路见AC代码。

AC代码

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> vs; //求len为数字字符串的长度也对应字母字符串的个数
int len = strlen(digits.c_str());
if (len <= 0 )
return vs; if (len == 1)
{
string str = letters(digits[0]);
for (int i = 0; i < strlen(str.c_str()); i++)
{
string s = "";
s += str[i]; vs.push_back(s);
}
return vs;
} //前两个单独处理 string str1 = letters(digits[0]);
string str2 = letters(digits[1]);
for (int i = 0; i < strlen(str1.c_str()); i++)
{
for (int j = 0; j < strlen(str2.c_str()); j++)
{
string str = "";
str = str + str1[i] + str2[j];
vs.push_back(str);
}
} for (int i = 2; i < len; i++)
{
string str = letters(digits[i]);
vs = Combine(vs, str);
}
return vs;
} vector<string> Combine(const vector<string> &vs, const string &str2)
{
vector<string> v; int len = vs.size();
if (len <= 0)
return v; int len2 = strlen(str2.c_str()); for (int i = 0; i < len; i++)
{
string str = vs[i];
for (int j = 0; j < len2; j++)
{
v.push_back(str + str2[j]);
}
}//for return v;
} string letters(const char &num)
{
string str = ""; switch (num)
{
case '2':
str = "abc"; break;
case '3':
str = "edf"; break;
case '4':
str = "ghi"; break;
case '5':
str = "jkl"; break;
case '6':
str = "mno"; break;
case '7':
str = "pqrs"; break;
case '8':
str = "tuv"; break;
case '9':
str = "wxyz"; break;
default:
str = "";
}
return str;
}
};

GitHub测试程序源码

LeetCode (17)Letter Combinations of a Phone Number的更多相关文章

  1. LeetCode(17)Letter Combinations of a Phone Number

    题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...

  2. leetcode第18题--Letter Combinations of a Phone Number

    Problem: Given a digit string, return all possible letter combinations that the number could represe ...

  3. LeetCode(17):电话号码的字母组合

    Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...

  4. Leetcode(17)-电话号码的字母组合

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...

  5. Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)

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

  6. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

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

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

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

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

  9. Leetcode 17. Letter Combinations of a Phone Number(水)

    17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...

随机推荐

  1. 为页面添加favicon

    <link rel="shortcut icon" href="favicon.ico" /> 还有另一种写法,但是IE对它的支持不够好: < ...

  2. 浅析 Spark Shuffle 内存使用

    在使用 Spark 进行计算时,我们经常会碰到作业 (Job) Out Of Memory(OOM) 的情况,而且很大一部分情况是发生在 Shuffle 阶段.那么在 Spark Shuffle 中具 ...

  3. IIS6配置FastCGI遇到ERROR5的解决方法

    FastCGI Error The FastCGI Handler was unable to process the request. ------------------------------- ...

  4. JVM线程与Linux内核线程的映射[转]

    Linux从内核2.6开始使用NPTL (Native POSIX Thread Library)支持,但这时线程本质上还轻量级进程. Java里的线程是由JVM来管理的,它如何对应到操作系统的线程是 ...

  5. 洛谷P2774 方格取数问题(最小割)

    题意 $n \times m$的矩阵,不能取相邻的元素,问最大能取多少 Sol 首先补集转化一下:最大权值 = sum - 使图不连通的最小权值 进行黑白染色 从S向黑点连权值为点权的边 从白点向T连 ...

  6. java (给出年月日,计算该日是该年的第n天 )

    package com.ywx.testdemo01; import java.util.Scanner; /** * 题目:给出年月日,计算该日是该年的第n天 * @author yangwenxu ...

  7. 【HEVC简介】ALF-Adative Loop Filter

    由于HEVC在HM4.0之后,就把ALF去掉,所以ALF的介绍是基于AVS2. <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com ...

  8. Mac OSX简单使用中会用到的

    选择操作系统(例如选择BootCamp分区的Windows):开机按住Option键直到磁盘图标出现后选择. 忘记本地账号密码:按着Command+R开机选择Recovered启动打开终端输入re ...

  9. 洛谷 U10206 Cx的治疗

    题目背景 「Cx的故事」众所周知,Cx是一个宇宙大犇.由于Cx在空中花园失足摔下,导致他那蕴含着无穷智慧的大脑受到了严重的损伤,许多的脑神经断裂.于是,Cx的wife(有么?)决定请巴比伦最好的医师治 ...

  10. WEB前端研发工程师编程能力成长之路(2)(转)

    WEB前端研发工程师编程能力成长之路(2)   四.[入微] 最强解决方案.你能够走在需求的前面,将当前需求里有的.没有直接提出来的.现在暂时没有但将来可能有的等等,及前端编程潜规则等各个方方面面都综 ...