一、题目说明

题目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. xshell 链接虚拟机

    1.在虚拟机上添加网络适配器,选择仅主机模式 2. 2.启用本机的网络连接 3.在虚拟机上开启ssh服务 首先看下22端口有没开放,如果没有的话需要开启 service start sshd 4.在虚 ...

  2. mybatis--使用接口注解的方式实现Helloword

    首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下: create database my; use my; create table user( id ...

  3. Educational Codeforces Round 80 (Rated for Div. 2)C(DP)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ; ][],temp[][]; int ...

  4. nginx配置指令auth_basic、auth_basic_user_file及相关知识

    参考链接 https://www.jianshu.com/p/1c0691c9ad3c auth_basic_user_file 所需要的密码,可以通过如下方法生成 1)安装htpasswd (yum ...

  5. 每天进步一点点------基础实验_08_触发器 :D、T触发器各一

    /********************************************************************************* * Company : * Eng ...

  6. NTP网络授时服务器部署及网络时钟同步设置说明

    NTP网络授时服务器部署及网络时钟同步设置说明 NTP网络授时服务器部署及网络时钟同步设置说明  本文由安徽京准科技提供@请勿转载. 一.前言 1.NTP简介 NTP是网络时间协议(Network T ...

  7. PHP转换oracle数据库的date类型

    今天圣诞节啊,圣诞节快乐啊! 最近遇到一个很纠结的事,就是我在plsql里面查的是这样的,很正常, 但是我用程序查出来就是这样的,啊啊啊,真是崩溃啊 但是我传数据需要上面那种格式,而且我对oracle ...

  8. 数制的运用-CodeForces - 535B

    题解: 因为每一位只可能是4或者7,可以类比二进制的思想. 基数为2,每一位的权值为2i-1:数字4表示的大小为1*2i-1:数字7表示的大小为2*2i-1. 将给定的n按照这种方法进行分解,求和.即 ...

  9. Spring Security 源码分析 --- WebSecurity

    概述 spring security 源码分析系列文章. 源码分析 我们想一下,我们使用 ss 框架的步骤是怎么样的. @Configuration @EnableWebSecurity @Enabl ...

  10. 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))

    题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...