1. 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']=" "; }
};

最新文章

  1. 编写可维护的CSS
  2. iOS GCD 编程小结
  3. SQL分页查询
  4. linux上创建ftp服务器下载文件///使用AWS服务器作为代理,下载sbt相关的包
  5. [转] Asp.net vNext webapi 自托管
  6. 遍历、显示ftp下的文件夹和文件信息
  7. 服务器上搭建spark开发环境
  8. 100个iOS开发/设计程序员面试题汇总,你将如何作答?
  9. java中连接postgresql基本代码
  10. 循环小数 UVa202
  11. 鸟哥私房菜--第1章 Linux 是什么
  12. ML: 聚类算法R包-模糊聚类
  13. cs231n spring 2017 lecture7 Training Neural Networks II 听课笔记
  14. 【noip模拟】tree
  15. __http原理__HTTP 协议简介
  16. asp。net内置委托
  17. Robot Framework脚本在jenkins执行完之后无法查看日志
  18. Some elementary algorithms on discrete differential geometry(DDGSpring2016 Demos)
  19. sparkStreaming运行yarn,提交任务报错
  20. ArcGIS Runtime SDK for iOS之符号和渲染

热门文章

  1. react 生命周期 个人见解
  2. Seata的一些概念
  3. Linux上的多进程间通信机制
  4. Python ImportError: cannot import name ABC
  5. Python ValueError: Attempted relative import in non-package Relative import相对引用 错误
  6. 回文链表 牛客网 程序员面试金典 C++ Python
  7. spring-cloud-square源码速读(retrofit + okhttp篇)
  8. spring security之 默认登录页源码跟踪
  9. USB3.0 转USB3.0
  10. Swift-Framework Error(一)桥接文件