45-Letter Combinations of a Phone Number
- Letter Combinations of a Phone Number My Submissions QuestionEditorial Solution
Total Accepted: 78554 Total Submissions: 273286 Difficulty: Medium
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”].
Submission Details
25 / 25 test cases passed.
Status: Accepted
Runtime: 0 ms
思路:依次迭代,比较简单
class Solution {
public:
vector<string> letterCombinations(string digits) {
int n = digits.size();
vector<string> res;
map<char,string> nmap;
init_map(nmap);
for(int i=0;i<n;++i){
if(digits[i]=='1')continue;//1代表空,无效数字越过
string word= nmap[digits[i]];
vector<string> sw,stmp;
for(int j=0;j<word.size();++j){
vector<char> vs;
vs.push_back(word[j]);
vs.push_back('\0'); //特别注意char* 转string
sw.push_back(vs.data());
for(int k=0;k<res.size();++k)
stmp.push_back(res[k]+sw[j]);
}
if(res.size()==0)res = sw;
else res = stmp;
}
return res;
}
void init_map(map<char,string> &nmap)
{
nmap['1']="";nmap['2']="abc";nmap['3']="def";
nmap['4']="ghi";nmap['5']="jkl";nmap['6']="mno";
nmap['7']="pqrs";nmap['8']="tuv";nmap['9']="wxyz";
nmap['0']=" ";
}
};
45-Letter Combinations of a Phone Number的更多相关文章
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 69. 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 ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- leetcode-algorithms-17 Letter Combinations of a Phone Number
leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- Letter Combinations of a Phone Number - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Letter Combinations of a Phone Number - LeetCode 注意点 可以不用按字典序排序 解法 解法一:输入的数字逐 ...
- 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 ...
随机推荐
- BUAA_2020_软件工程_结对项目作业
项目 内容 这个作业属于哪个课程 班级博客 这个作业的要求在哪里 作业要求 我在这个课程的目标是 掌握软件工程的思路方法 这个作业在哪个具体方面帮助我实现目标 学习结对编程 教学班级 006 项目地址 ...
- 有了 HTTP 协议,为什么还需要 Websocket?
WebSocket 是一种基于 TCP 连接上进行全双工通信的协议,相对于 HTTP 这种非持久的协议来说,WebSocket 是一个持久化网络通信的协议. 它不仅可以实现客户端请求服务器,同时可以允 ...
- sort方法和自定义比较器的写法
摘要 在做一些算法题时常常会需要对数组.自定义对象.集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法.对自定义对象排序 ...
- HashMap 中的一个“坑”!
最近公司新来了一个小伙伴,问了磊哥一个比较"奇怪"的问题,这个问题本身的难度并不大,但比较"隐蔽",那究竟是什么问题呢?接下来我们一起来看. 起因 最近公司 ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第八篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...
- Dao、Controller、Service三层的结构划分
Java Web基础--Controller+Service +Dao三层的功能划分(摘取自网络)1. Controller/Service/DAO简介: Controller是管理业务( ...
- 9组-Alpha冲刺-1/6
一.基本情况 队名:不行就摆了吧 组长博客:https://www.cnblogs.com/Microsoft-hc/p/15526668.html 小组人数: 8 二.冲刺概况汇报 谢小龙 过去两天 ...
- Qt分析:Qt中的两种定时器
QTimer类的定时器 QTimer类定时器是QObject类定时器的扩展版或者说升级版,因为它可以提供更多的功能.比如说,它支持单次触发和多次触发. 使用QTimer类定时器的步骤: (1)创建一个 ...
- Django笔记&教程 4-1 模型(Models)介绍
Django 自学笔记兼学习教程第4章第1节--模型(Models)介绍 点击查看教程总目录 参考:https://docs.djangoproject.com/en/2.2/topics/db/mo ...
- 使用.NET5、Blazor和Electron.NET构建跨平台桌面应用
Electron.NET是一个嵌入了ASP.NET Core的Electron的封装,通过Electron.NET可以构建基于.NET5的跨平台的桌面应用,使得开发人员只需要使用ASP.NET Cor ...