题目链接

Letter Combinations of a Phone Number - LeetCode

注意点

  • 可以不用按字典序排序

解法

解法一:输入的数字逐个处理,在已经生成的字符串后面追加该当前数字对应的所有字符。时间复杂度为O(n)

class Solution {
public:
vector<string> letterCombinations(string digits) {
vector<string> ans,temp_ans;
map<char,vector<string>> mp;
mp['2']={"a","b","c"};
mp['3']={"d","e","f"};
mp['4']={"g","h","i"};
mp['5']={"j","k","l"};
mp['6']={"m","n","o"};
mp['7']={"p","q","r","s"};
mp['8']={"t","u","v"};
mp['9']={"w","x","y","z"};
int i,j,k,n = digits.length();
for(i = 0;i < n;i++)
{
int size = ans.size();
vector<string> v = mp[digits[i]];
for(j = 0;j < v.size();j++)
{
for(k = 0;k < size;k++)
{
string temp=ans[k]+v[j];
temp_ans.push_back(temp);
}
if(size==0) temp_ans.push_back(v[j]);
}
swap(ans,temp_ans);
temp_ans.clear();
}
return ans;
}
};

小结

  • 代码网上看来的,自己重新打了一遍。相比其他博客,这个算是最好理解的。

Letter Combinations of a Phone Number - LeetCode的更多相关文章

  1. Letter Combinations of a Phone Number leetcode java

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

  2. Letter Combinations of a Phone Number——LeetCode

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

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

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

  4. LeetCode解题报告—— Container With Most Water & 3Sum Closest & Letter Combinations of a Phone Number

    1.  Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a ...

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

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

  6. 【leetcode】Letter Combinations of a Phone Number

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

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

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

  8. LeetCode: Letter Combinations of a Phone Number 解题报告

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

  9. [LeetCode]Letter Combinations of a Phone Number题解

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

随机推荐

  1. NO--19 微信小程序之scroll-view选项卡与跳转(二)

    本篇为大家介绍为何我们在最后做交互的时候,并没有使用上一篇讲的选项卡的效果.   scroll-view与跳转.gif (如无法查看图片,还请翻看上一篇!) 大家注意看,在我点击跳转后,首先能看到的是 ...

  2. JS对象,获取key和value

    var peopleArray=[] var peopleobj={jiangyx: "姜艳霞", yeluosen: "叶落森"} for(let i in ...

  3. 01-numpy基础简介

    import numpy as np # ndarray ''' # 三种创建方式 1.从python的基础数据对象转化 2.通过numpy内置的函数生成 3.从硬盘(文件)读取数据 ''' # 创建 ...

  4. SpringBoot初始教程之Redis集中式Session管理

    1.介绍 有关Session的管理方式这里就不再进行讨论,目前无非就是三种单机Session(基于单机内存,无法部署多台机器).基于Cookie(安全性差).基于全局的统一Session管理(redi ...

  5. Visual Studio win平台 AI环境搭建

    内容提要:我觉得难点主要出在下载上,程序跑的都挺流畅的.下载有时会失败. 1.下载安装git.这一步主要为了下载示例和自动安装环境的python代码,直接去github上用网页下载也是一样的,git不 ...

  6. Task 9 从用户界面和体验分析“360极速浏览器”

    我目前使用的浏览器是360极速浏览器,下面将针对用户界面.记住用户选择.短期刺激.长期使用的好处坏处.不要让用户犯简单的错误四个方面对其进行评估: 1.用户界面: 01 可视性原则--网络没有连接或者 ...

  7. Class 2 四则运算2的设计思路

    设计思路 1.主函数中有一个大的for循环,用户可以一直随机得到相应题目.在嵌套一个循环,其可以直接确定题目数量:定义两个变量,分别作为四则运算的两个运算数,用随机数函数得到两个数值:再利用随机生成函 ...

  8. Scapy之ARP询问

    引言 校园网中,有同学遭受永恒之蓝攻击,但是被杀毒软件查下,并知道了攻击者的ip也是校园网.所以我想看一下,这个ip是PC,还是路由器. 在ip视角,路由器和pc没什么差别. 实现 首先是构造arp报 ...

  9. 【CSAPP笔记】7. 汇编语言——过程调用

    一个过程调用包括将数据(以参数和返回值的形式)与控制从代码的一部分传递到另一部分.除此之外,在进入时为过程的局部变量分配空间,在退出的时候释放这些空间.数据传递.局部变量的分配和释放通过操纵程序栈来实 ...

  10. #1490 : Tree Restoration

    微软 2017春招真题 题目 There is a tree of N nodes which are numbered from 1 to N. Unfortunately, its edges a ...