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 ...
随机推荐
- AtCoder Beginner Contest 058 ABCD题
A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...
- JAVA平台的理解
主题: JAVA是解释执行还是编译执行? 我的答案 : 混合模式 闲谈 : 1. JAVA(write once,run anywhere): 2. GC(Garbagae Collection), ...
- Apache Kylin Cube 的存储
不多说,直接上干货! 简单的说Cuboid的维度会映射为HBase的Rowkey,Cuboid的指标会映射为HBase的Value. Cube映射成HBase存储 如上图原始表所示:Hive表有两个维 ...
- 学JAVA第二十四天,Set集合与StringBuilder
下面的内容需要慢慢看,因为,我的语言表达能力不是很好 首先说Set把,Set集合是一个无序且不允许重复的集合,而且查找效率也是快的可怕的. 但是,有些时候,我们必须要用储存多个相同的值时,Set也是可 ...
- Google的Java编程风格指南
作者:Hawstein出处:http://hawstein.com/posts/google-java-style.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Cre ...
- 正确使用MySQL JDBC setFetchSize()方法解决JDBC处理大结果
一直很纠结,Oracle的快速返回机制,虽然结果集很多,可是它能很快的显示第一个结果,虽然通过MYSQl的客户端可以做到,但是通过JDBC却不行. 今天用了1个多小时,终于搞定此问题,希望对广大Jav ...
- springboot的多个配置文件的关系
一般我们在使用springboot时会用到多个环境下的配置文件,例如 application-dev.yml:开发环境 application-uat.yml:用户验收测试环境 application ...
- android 图片叠加效果——两种方法的简介与内容 ,带解决Immutable bitmap passed to Canvas constructor错误
第一种是通过canvas画出来的效果: public void first(View v) { // 防止出现Immutable bitmap passed to Canvas constructor ...
- codevs 3070 寻找somebody4(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 有一天.....sb不见了,有个人要去找他..他发现sb在一个杨辉三角里.. ...
- 数据库管理系统X
大部分DBMS提供数据定义语言DDL(Data Definition Language)和数据操作语言DML(Data Manipulation Language),供用户定义数据库的模式结构与权限约 ...