题目

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. 在vue组件的stylus样式总 取消search类型的input按钮中默认样式

    在vue组件的stylus样式中 取消search类型的input按钮中默认样式 记录一个坑 环境 Vue组件 使用了Stylus的CSS风格. 问题 input输入框使用了 type="s ...

  2. 系统中同时存在python2和python3时 pip有时候更新后会报错 解决安装的方法如下

    官网原链接:https://pip.pypa.io/en/stable/installing/ Installation Do I need to install pip? pip is alread ...

  3. Hu矩

    close all; clear all; I1=imread('lena.bmp'); angle=; T=[cos(angle),sin(angle),;-sin(angle),cos(angle ...

  4. [转]2010 Ruby on Rails 書單 與 練習作業

    原帖:http://wp.xdite.net/?p=1754 ========= 學習 Ruby on Rails 最快的途徑無非是直接使用 Rails 撰寫產品.而這個過程中若有 mentor 指導 ...

  5. AJPFX总结集合的概念

    //java 中集合的概述==========================================================        集合的概念:              为 ...

  6. AJPFX谈Java 性能优化之基本类型 vs 引用类型

    ★名词定义 先明确一下什么是“基本类型”,什么是“引用类型”. 简单地说,所谓基本类型就是 Java 语言中如下的8种内置类型: booleancharbyteshortintlongfloatdou ...

  7. LN : leetcode 730 Count Different Palindromic Subsequences

    lc 730 Count Different Palindromic Subsequences 730 Count Different Palindromic Subsequences Given a ...

  8. iOS infoq资料架构设计漫谈

    http://www.infoq.com/cn/ios/?utm_source=infoq&utm_medium=header_graybar&utm_campaign=topic_c ...

  9. Android Learning Note -- AsyncTask浅尝

    AsyncTask 实现原理 AsyncTask是Android提供的轻量级异步类,可以直接继承AsyncTask在类中实现异步操作,并提供接口反馈当前的异步执行程度(通过接口实现UI进度更新),最后 ...

  10. Tomcat配置Oracle数据源

    开发环境:Eclipse luna.tomcat 7.Oracle 配置Oracle datasource步骤 第一步:打开tomcat目录下的 context.xml 文件,添加 Resource ...