刷题17. Letter Combinations of a Phone Number
一、题目说明
题目17. Letter Combinations of a Phone Number,题目给了下面一个图,输入一个字符串包括2-9,输出所有可能的字符组合。

如输入23所有可能的输出:
"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"
二、我的做法
这个题目,我思考了4个小时(惭愧严重超时了),做法如下:
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
class Solution{
public:
vector<string> letterCombinations(string s){
vector<string> res;
if(s.size()<1) return res;
int num = 1;
unordered_map<char,string> ump;
ump['2'] = "abc";
ump['3'] = "def";
ump['4'] = "ghi";
ump['5'] = "jkl";
ump['6'] = "mno";
ump['7'] = "pqrs";
ump['8'] = "tuv";
ump['9'] = "wxyz";
for(int i=0;i<s.size();i++){
switch(s[i]){
case '2':
case '3':
case '4':
case '5':
case '6':
case '8':
num *= 3;
break;
case '7':
case '9':
num *=4;
break;
}
}
for(int i=0;i<num;i++){
res.push_back("");
}
int curNum = num;
for(int j=0;j<s.size();++j){
char curr = s[j];
string curStr = ump[curr];
curNum /= curStr.size();
for(int i=0;i<num;i++){
res[i].push_back(curStr[i / curNum % curStr.size()]);
}
}
return res;
}
};
int main(){
Solution s;
// vector<string> r = s.letterCombinations("234");
// for(vector<string>::iterator it=r.begin();it!=r.end();++it){
// cout<<*it<<" ";
// }
// cout<<endl;
vector<string> r = s.letterCombinations("8");
for(vector<string>::iterator it=r.begin();it!=r.end();++it){
cout<<*it<<" ";
}
return 0;
}
这个是我第一次,做“完美”的代码。臭美一下!
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Letter Combinations of a Phone Number.
Memory Usage: 8.4 MB, less than 100.00% of C++ online submissions for Letter Combinations of a Phone Number.
三、更优化的做法
第一次可以自豪的说一句,这个就是最优化的代码了。哈哈!
刷题17. Letter Combinations of a Phone Number的更多相关文章
- [刷题] 17 Letter Combinations of a Phone Number
要求 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合 1 不对应任何字母 示例 输入:"23" 输出:["ad", "ae&q ...
- [LeetCode][Python]17: Letter Combinations of a Phone Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...
- Leetcode 17. Letter Combinations of a Phone Number(水)
17. Letter Combinations of a Phone Number Medium Given a string containing digits from 2-9 inclusive ...
- 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合
Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...
- 17. Letter Combinations of a Phone Number
题目: Given a digit string, return all possible letter combinations that the number could represent. A ...
- Leetcode 17.——Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- 【一天一道LeetCode】#17. Letter Combinations of a Phone Number
一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...
- [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
随机推荐
- C++转换构造函数和隐式转换函数
今天是第一次听到C++还有个转换构造函数,之前经常见到默认构造函数.拷贝构造函数.析构函数,但是从没听说过转换构造函数,隐式转换函数也是一样,C++的确是够博大精深的,再次叹服! 其 ...
- 2.6 XML配置:XML文件节点说明
来源:http://blog.csdn.net/five3/article/details/25907693 TestNG的DTD检查文件:http://testng.org/testng-1.0.d ...
- vue生命周期钩子函数详解
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_35585701/article/ ...
- Java中查询某个日期下所有时间段的数据
除了利用时间段进行查询外,还有一个方法: 利用mybatis中的函数,将datetime转为date <if test="purch_date!= null and purch_dat ...
- 数据库程序接口——JDBC——API解读第二篇——执行SQL的核心对象
结构图 核心对象 Statement Statement主要用来执行SQL语句.它执行SQL语句的步骤为: 第一步:创建statement对象. 第二步:配置statement对象,此步骤可以忽略. ...
- ftrace使用
前段时间遇到个问题,有个后继音频数据处理线程调度不及时导致音频输出延迟,音视频不同步. 因此从系统上入手,采用ftrace进行debug,来看有哪些线程会去抢占音频数据处理线程. ftrace 提供了 ...
- 每天进步一点点------H.264学习 (一)
分三个阶段学习1.第一个阶段: 学习H.264,首先要把最基本最必要的资料拿在手里.这些资料包括:标准文档+测试模型+经典文章,在本FTP中能找到.首先看 <H.264_MPEG-4 Part ...
- 多租户SaaS平台的数据库方案
1.1 多租户是什么 多租户技术(Multi-TenancyTechnology)又称多重租赁技术:是一种软件架构技术,是实现如何在多用户环境下 (此处的多用户一般是面向企业用户)共用相同的系统或程序 ...
- jmeter实现服务器端后台接口性能测试
实现目的 在进行服务器端后台接口性能测试时,需要连接到Linux服务器端,然后通过命令调用socket接口,这个过程就需要用到jmeter的SSH Command取样器实现了. 脚本实现 设置CSV ...
- socket udp编程的一些积累的记录
接了个小活,要求写udp的客户端,循环接收服务端的固定的指令并显示数据 我设计的逻辑是,用户在界面输入框输入服务器ip.端口,随后udp连接,开启线程循环接收,接收指令,解析成数据,存在结构体的lis ...