Letter Combinations of a Phone Number - LeetCode
题目链接
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的更多相关文章
- Letter Combinations of a Phone Number leetcode java
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Letter Combinations of a Phone Number——LeetCode
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- 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 ...
- Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
[Leetcode]17. 电话号码的字母组合(Letter Combinations of a Phone Number) 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组 ...
- 【leetcode】Letter Combinations of a Phone Number
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode: Letter Combinations of a Phone Number 解题报告
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- [LeetCode]Letter Combinations of a Phone Number题解
Letter Combinations of a Phone Number: Given a digit string, return all possible letter combinations ...
随机推荐
- 2018Java年底总结
一年又过去了,这是我的第二年的JAVA开发,总感觉有很多想说的,可惜语言组织能力着实一般,以下列举一些今年的总结. 1.首先告诫一下新入行或者新入职经验不多的小伙伴,写sql的时候根据业务能单表就单表 ...
- 【坚持】Selenium+Python学习之从读懂代码开始 DAY4
2018/05/21 [生成器详解:廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d ...
- window搭建私有云,只要几分钟
本文介绍如何在window搭建私有云网盘. 工具/原料:一台window系统电脑或者window服务器(vps),Xampp 安装包,可道云kodexplorer安装包 第一步,xampp安装 1.官 ...
- MyCat安装与测试教程 超详细!
MyCat安装与测试教程 超详细! MyCat基础知识 一.什么是MYCAT? 1. 一个彻底开源的,面向企业应用开发的大数据库集群 2. 支持事务.ACID.可以替代MySQL的加强版数据库 3. ...
- Spring Data REST PATCH请求远程代码执行漏洞(CVE-2017-8046) 本地复现方法
#1背景 Spring Data REST是Spring Data项目的一部分,可以轻松地在Spring Data存储库之上构建超媒体驱动的REST Web服务. 恶意的PATCH请求使用精心构造 ...
- node child_process模块
NodeJs是一个单进程的语言,不能像Java那样可以创建多线程来并发执行.当然在大部分情况下,NodeJs是不需要并发执行的,因为它是事件驱动性永不阻塞.但单进程也有个问题就是不能充分利用CPU的多 ...
- [shell] bash数组(for时排序)
for处理时会自动把顺序按A-Z排序了 [root@XM-v106 ~]# bash b.sh A -> B -> C -> D -> E -> [root@XM-v10 ...
- 【探路者】Final发布
[探路者]团队项目final发布:贪吃蛇 [探路者]贪吃蛇 final发布展示(视频)链接: http://v.youku.com/v_show/id_XMzIxMDM2MTQ1Ng==.html?s ...
- Scrum Meeting 10.26
1.会议内容 姓名 今日任务 明日任务 预估时间(h) 徐越 学习服务器配置 配置SQLserver 4 卞忠昊 阅读代码 找上届代码的bug 3 武鑫 查阅资料 查阅资料,各种app的界面设计 3 ...
- [2017BUAA软工]结对项目:数独扩展
结对项目:数独扩展 1. Github项目地址 https://github.com/Slontia/Sudoku2 2. PSP估计表格 3. 关于Information Hiding, Inter ...