一、题目说明

题目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. 出现xxxtbox问题又有集群网络等问题时的解决

    出现xxxtbox问题又有集群网络等问题时的解决 集群环境本身问题引发 重新rancher上删除集群,正常构建集群成功,环境就没问题,否则由于环境问题怎么找解决方案都有问题

  2. sqli-libs(32-37(宽字节注入)关)

    补充知识:宽字节注入 定义:GB2312.GBK.GB18030.BIG5.Shift_JIS等这些都是常说的宽字节,实际上只有两字节.宽字节带来的安全问题主要是吃ASCII字符(一字节)的现象,即将 ...

  3. vue路由,ajax,element-ui

    复习 """ 1.vue项目环境: node => npm(cnpm) => vue/cli 2.vue项目创建: vue create 项目 在pychar ...

  4. Linux 内核内存池

    内核中经常进行内存的分配和释放.为了便于数据的频繁分配和回收,通常建立一个空闲链表——内存池.当不使用的已分配的内存时,将其放入内存池中,而不是直接释放掉. Linux内核提供了slab层来管理内存的 ...

  5. 第一篇 Python中一切皆对象

  6. Bugku-CTF加密篇之一段Base64

    一段Base64   flag格式:flag{xxxxxxxxxxxxx}    

  7. HyperLedger Fabric 资料网址大全

    BLOCKCHAIN FOR DEVELOPERS 官方网址 i. 这个网址是ibm给的测试网址,注册进去就可以设置4个节点的区块链,而且有智能合约可以测试 区块链和HyperLedger开源技术讲堂 ...

  8. python 的集合

    set  的特点: 把不同的元素组合在一起,元素值必须是可哈希不可变的 set 的创建 s = set ('alex li') print(s) 表现形式:去重 {'e', 'i', ' ', 'l' ...

  9. 吴裕雄 python 机器学习——模型选择验证曲线validation_curve模型

    import numpy as np import matplotlib.pyplot as plt from sklearn.svm import LinearSVC from sklearn.da ...

  10. "exit"未定义标签 问题

    找了两个多小时,最后才发现是版本问题.因为是网上下的代码,可能用的版本比较高,而我自己的是2.4.10版本的opencv,所以正确的代码应该是如下: CV_Error(CV_StsBadArg,&qu ...