一、题目说明

题目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的更多相关文章

  1. [刷题] 17 Letter Combinations of a Phone Number

    要求 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合 1 不对应任何字母    示例 输入:"23" 输出:["ad", "ae&q ...

  2. [LeetCode][Python]17: Letter Combinations of a Phone Number

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 17: Letter Combinations of a Phone Numb ...

  3. 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 ...

  4. 《LeetBook》leetcode题解(17):Letter Combinations of a Phone Number[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  5. [LeetCode] 17. Letter Combinations of a Phone Number 电话号码的字母组合

    Given a string containing digits from 2-9inclusive, return all possible letter combinations that the ...

  6. 17. Letter Combinations of a Phone Number

    题目: Given a digit string, return all possible letter combinations that the number could represent. A ...

  7. Leetcode 17.——Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  8. 【一天一道LeetCode】#17. Letter Combinations of a Phone Number

    一天一道LeetCode (一)题目 Given a digit string, return all possible letter combinations that the number cou ...

  9. [leetcode]17. Letter Combinations of a Phone Number手机键盘的字母组合

    Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...

随机推荐

  1. liunx详解-2

    linux安装与配置 安装配置 虚拟机配置1G内存,1核CPU,50G硬盘,网络地址转换(NAT,主机作为路由构建内网) 镜像文件:http://mirror.nsc.liu.se/centos-st ...

  2. BFS与食物链条数

    最近学校生物在教能量流动,因此离不开食物网,也就离不开食物链. 那么问题来了,给出食物网(DAG),怎么求食物链条数呢? 怎么来的?首先,找到同时被2个以上生物捕食的被捕食者,求出:能量来源X(能量去 ...

  3. 断点调试,issubclass和ininstance的使用

    一等公民 只要可以把一个东西赋值给一个变量,这个东西就叫一等公民 断点调试 在想要加断点的地方用鼠标点击一下,你会看到一个红色圆圈 变红的地方,程序执行到,就会暂停 断电应该加载报错之前 绿色箭头表示 ...

  4. Bug搬运工-CSCvg37458:ISR4K goes into booting loop with "flash:" in boot statement

    ISR4K升级的时候要注意了! 很可能会碰到如下的问题: ISR4K goes into booting loop with "flash:" in boot statement ...

  5. AD 快捷键设置

    TAA 设置自动标记原件位号 AR 设置元件向右对齐 AL 设置元件向左对齐 MS 移动所选择 CO 错误报告设置 CTRL + w 设置电气线 PN 添加net label TG 打开封装管理器 P ...

  6. VSCode的安装和使用

    VSCode的安装和使用 1.先到下载地址选择适合自己系统的VSCode安装软件 https://code.visualstudio.com/#alt-downloads 以上分别是Windows , ...

  7. 用Python给头像加上圣诞帽或圣诞老人小徽章

    随着圣诞的到来,想给给自己的头像加上一顶圣诞帽.如果不是头像,就加一个圣诞老人陪伴. 用Python给头像加上圣诞帽,看了下大概也都是来自2017年大神的文章: https://zhuanlan.zh ...

  8. source insight 编译后出现停止工作解决方法

    解决方法: 工程的路径不要有中文,都用英文

  9. ubuntu 先安装php再安装apache后,php不解析

    本来服务器是nginx ,把他改成了apache, 安装apache, sudo apt-get updatesudo apt-get install apache2 然后访问ip,不成功 vim / ...

  10. TM1638控制

    原理图图纸: 显示控制与读按键与寄存器的对应 驱动代码:码云: