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 ...
随机推荐
- 通过Nacos动态刷新Spring Cloud Gateway的路由
通过Nacos动态刷新Spring Cloud Gateway的路由 一.背景 二.解决方案 三.实现功能 四.实现步骤 1.网关服务的实现 1.pom文件 2.bootstrap.yml配置文件 3 ...
- 万维网www与HTTP协议
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105901440 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 全志TinaLinux编译错误fatal error: unicode/ucnv.h: No such file or directory
今天开始正式干活了 拿到一个全志Tina的板子还有一个SDK压缩包,要求我这周(只剩一天半...)就要把sdk编译通过并且把板子跑起来. 还特别跟我说他们试了下这个sdk编译没法通过,会报错... 竟 ...
- Django(74)drf-spectacular自动生成接口文档
介绍 drf-spectacular是为Django REST Framework生成合理灵活的OpenAPI 3.0模式.它可以自动帮我们提取接口中的信息,从而形成接口文档,而且内容十分详细,再也不 ...
- Navicat15 For Mysql最新版完美破解图文教程(支持Win和Mac)
Navicat15 For Mysql最新版完美破解 欢迎关注博主公众号[跟着Mic学架构],专注于分享Java领域技术干货,回复关键字 [面试资料] 可以获得海量面试资料. 申明,本教程 Navic ...
- restTemple发送请求、上传文件(@LoadBalanced微服务调用及url调用)
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Co ...
- WebJar的打包和使用
前言 WebJar官网:https://www.webjars.org/,对于任何与Servlet 3兼容的容器,WEB-INF/lib目录中的webjar都会自动作为静态资源提供.这是因为WEB-I ...
- Python 爬取 豆瓣
... import urllib.request import time from bs4 import BeautifulSoup def url_open(url): response = ur ...
- Windows11 如何让开始菜单出现休眠选项(Windows11如何开启休眠功能?)Windows家庭版
1. Windows11新版的菜单找不到调休眠的选项了,所以我们可以用win+r键调出运行,输入control,回车调出「控制面板」 配图: 2. 我的电脑系统是家庭版的Windows11,其它版本这 ...
- SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档
本章介绍Spring Cloud Gateway网关如何集成knife4j,通过网关聚合所有的Swagger微服务文档 1.gitegg-gateway中引入knife4j依赖,如果没有后端代码编 ...