LeetCode (17)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”].
分析
本题目要求求出多个字符串按字母全排得到的字符串集合。
我们知道求两个字符串的字母全排是简单的,只需要两次遍历,按照字母组合即可。
那么如何求多个字符串的全排呢?尤其是字符串的个数还是不固定的,对此(字符串大于等于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;
}
};
LeetCode (17)Letter Combinations of a Phone Number的更多相关文章
- LeetCode(17)Letter Combinations of a Phone Number
题目如下: Python代码: class Solution(object): def letterCombinations(self, digits): """ :ty ...
- leetcode第18题--Letter Combinations of a Phone Number
Problem: Given a digit string, return all possible letter combinations that the number could represe ...
- LeetCode(17):电话号码的字母组合
Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23& ...
- Leetcode(17)-电话号码的字母组合
给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:[&quo ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
随机推荐
- Jenkins自动化部署——持续交付
感谢之前带领过我的leader,让我能够知道什么是好的开发方法. 在很早之前就接触过敏捷开发.什么是敏捷开发,简单来说就是让软件可靠地,快速地发布出来的一种开发方法和技巧. 而敏捷开发中有许多的实践, ...
- Apache Zeppelin是什么?
Apache Zeppelin提供了web版的类似ipython的notebook,用于做数据分析和可视化.背后可以接入不同的数据处理引擎,包括spark, hive, tajo等,原生支持scala ...
- Codeforces 1144F(二分染色)
发现奇环不可行,偶环可行,考虑二分图.然后染色,方向全都从一种指向另一种就可以了,随意. ; int n, m, color[maxn]; vector<int> vc[maxn]; ve ...
- Discovering Gold LightOJ - 1030 || 概率与期望求法区别
#include<cstdio>//wrong_codes #include<algorithm> using namespace std; ],anss; ],T,TT,n, ...
- Alt+数字 输入特殊字符
前言: 按住Alt键不放,再按(小键盘的)数字键,然后放开就可以输入特殊字符. 起始 终止 字符类别 0 255 基本与ASCII 码表对应 42657 42680 大写希腊字母 ...
- 转--oracle查看允许的最大连接数和当前连接数等信息
两个参数间的关系:sessions=1.1*processes+5 目前总结的语句,在查看数据的连接情况很有用,写完程序一边测试代码一边查看数据库连接的释放情况有助于分析优化出一个健壮的系统程序来. ...
- 微信小程序 开放能力学习
1. 用户信息小程序登录使用微信的个人信息快速搭建用户体系,登录逻辑:小程序向微信获取code 给服务端生成用户. 说明1. 小程序端调用 wx.login() 获取临时登录凭证 code,并传到服务 ...
- VC++编译出错:LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
解决方法: 1.搜索C盘下的cvtres.exe,结果得到类似这样的列表: C:\Program Files\Microsoft Visual Studio 10.0\VC\bin C:\Window ...
- Quartz使用二 通过属性传递数据
上一篇介绍了通过context.getJobDetail().getJobDataMap()方式获取传递的数据,其实可以通过定义属性来传递参数 package org.tonny.quartz; im ...
- 自欺欺人的使用 NSTimer 销毁
自欺欺人的使用 NSTimer 销毁 Demo地址 1.NSTimer是要加到runloop中才会起作用. 常见的创建timer方式 // 第一种方式 @property (nonatomic , s ...