题目链接

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. TPO-22 C1 Complain about a biased article

    /* 加粗:语音部分 * 红色:单词部分 * 斜体:语法部分 * 下划线:信号词/句 */ 第 1 段 1.Listen to a conversation between a student and ...

  2. HTML6的10个高级新特性

    网络技术正趋向于发展为一个巨大的移动APP市场,在Web开发的革命浪潮中起着指示性作用,自HTML引入以来,应用程序变得So easy,web开发中运用先进技术也很容易处理各种复杂Bug. 作为专业的 ...

  3. JavaScript学习笔记(七)—— 再说函数

    第八章 函数 1 函数声明和函数表达式 差别一:函数声明:函数在执行代码前被创建:函数表达式是在运行阶段执行代码时创建: 差别二:函数声明创建一个与函数同名的变量,并让她指向函数:使用函数表达式,不给 ...

  4. ubuntu 相关软件设置

    软件篇 1. 网易云音乐软件 首先去官网下载网易云音乐客户端linux版,网址:http://music.163.com/#/download,选择linux版本,然后选择ubuntu 16.04(6 ...

  5. linux 常用反弹shell小记

    在渗透测试过程中由于防火墙和其它安全防御措施,很多服务器只能单向向外访问,不能被访问,我们常常需要反弹shell. 1.bash反弹shell 本地开启监听 nc -lvvp 受害主机命令 bash ...

  6. dirname命令详解

    基础命令学习目录首页 原文链接:https://blog.csdn.net/xiaofei125145/article/details/50620281 示例一 来自手册页的例子 $ dirname ...

  7. Scrum Meeting 11.09

    成员 今日任务 明日计划 用时 徐越 解决bug:可以重复点赞:answer被选为best answer后点赞数归零:首页不能正确显示问题的回复数.修改搜索功能的代码  继续测试相关app功能,如果达 ...

  8. 实验二 Java面向对象程序设计 20135321

    课程:Java程序设计   班级:1353    姓名:余佳源  学号:20135321 成绩:             指导教师:娄嘉鹏      实验日期:2015-5-8 实验密级:       ...

  9. SQLyog的基本使用

    [简介] SQLyog是mysql数据库的客户端软件 [基本使用] 1.连接mysql数据库 2.SQLyog的页面使用介绍 3.基本的数据库命令 1) use命令 切换数据库 2) unsigned ...

  10. 【每日scrum】第一次冲刺day5

    请教以前做过类似软件的同学,受益匪浅,启发自己