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 ...
随机推荐
- hashCode及HashMap中的hash()函数
一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...
- 《杜增强讲Unity之Tanks坦克大战》5-子弹
5 子弹 本节的任务是创建子弹的Prefab image 首先从Model/Shell找到子弹的模型,拖入Hierarchy中,添加刚体组件,所有属性默认值. 添加Capsule Collider ...
- DMA-Direct Memory Access
什么是? DMA 首先它是一种数据的传输方式:其次传输包括从设备到内存.内存到存储.内存到内存等:最后,它是在不经过cpu的情况下实现数据传输.. DAM主要有共享缓存DMA,Scatter/Gath ...
- 零基础学python之构建web应用(入门级)
构建一个web应用 前面的学习回顾: IDLE是Python内置的IDE,用来试验和执行Python代码,可以是单语句代码段,也可以是文本编辑器中的多语句程序. 四个内置数据结构:列表.字典.集合和元 ...
- Netty源码分析第8章(高性能工具类FastThreadLocal和Recycler)---->第7节: 获取异线程释放的对象
Netty源码分析第八章: 高性能工具类FastThreadLocal和Recycler 第七节: 获取异线程释放的对象 上一小节分析了异线程回收对象, 原理是通过与stack关联的WeakOrder ...
- Hyperledger Fabric 1.0.1至Hyperledger Fabric 1.0.5所升级的内容及修复的问题
基础更新 各版本每次迭代都会有一些基础更新内容,如文档修改覆盖.测试用例完善.用户体验改进及删除冗余无效代码等… 下面分类介绍的是一些版本迭代的重要更新内容,因个人实操和理解有限,部分更新并未明确,如 ...
- access和MySQL mssql
Access.MSSQL.MYSQL数据库之间有什么区别? Access数据库.MSSQL数据库.MYSQL数据库之间有什么区别? 不少企业和个人站长在网站制作时,会对数据库的概 ...
- 编译Android VNC Server
1,在如下地址checkout源代码,我checkout的版本为0.9.7http://code.google.com/p/android-vnc-server/source/checkout 2,在 ...
- Django之Models与ORM操作
一.models例子 from django.db import models class User(models.Model): """ 用户表 "" ...
- 输入一个URL到页面呈现其中发生的过程-------http过程详解
在我们点击一个网址,到它能够呈现在浏览器中,展示在我们面前,这个过程中,电脑里,网络上,究竟发生了什么事情. 服务器启动监听模式 那我们就开始了,故事其实并不是从在浏览器的地址栏输入一个网址,或者我们 ...