leetcode-algorithms-17 Letter Combinations of a Phone Number

Given a string containing digits from 2-9 inclusive, 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. Note that 1 does not map to any letters.

Example:

Input: "23"

Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

解法

通过数字对应的字符串,循环.

class Solution
{
public:
vector<string> letterCombinations(string digits)
{
std::vector<std::string> result;
if (digits.empty()) return result; result = {""};
std::vector<std::string> v = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
for(int i = 0 ; i < digits.size(); ++i)
{
int num = digits[i] - '0';
if(num < 0 || num > 9)
break; std::string cand = v[num];
if(cand.empty())
continue; std::vector<std::string> tmp = result;
result.clear();
for(int j = 0; j < tmp.size(); ++j)
{
for(int k = 0; k < cand.size(); ++k)
result.push_back(tmp[j] + cand[k]);
}
}
return result;
}
};

链接: leetcode-algorithms 目录

leetcode-algorithms-17 Letter Combinations of a Phone Number的更多相关文章

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

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

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

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

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

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

  4. 【LeetCode】17. Letter Combinations of a Phone Number 电话号码的字母组合

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:电话号码, 字母组合,回溯法,题解,leetcode, ...

  5. 【LeetCode】17. Letter Combinations of a Phone Number

    题目: 思路:设置两个List,一个存储当前层,一个存储最终层 public class Solution { public List<String> letterCombinations ...

  6. LeetCode:17. Letter Combinations of a Phone Number(Medium)

    1. 原题链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 题目要求 给定一 ...

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

  8. 刷题17. Letter Combinations of a Phone Number

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

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

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

  10. [leetcode 17]Letter Combinations of a Phone Number

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

随机推荐

  1. (转载)windows下安装配置Xampp

    XAMPP是一款开源.免费的网络服务器软件,经过简单安装后,就可以在个人电脑上搭建服务器环境.本文为大家介绍Windows中安装XAMPP(Apache+Mysql+PHP)及使用方法及其相关问题的总 ...

  2. Unity3D学习笔记(二十五):Json

    Json:使用固定的文本格式来存储和表示数据! 优点:简介,清晰,易于人的阅读和编写,易于机器的解析和生成. 类似:XML富文本形式 Json的键值对(类中的变量): Json的键值对是使用冒号来区分 ...

  3. GET和POST中文乱码的解决方法

    如果表单中含有中文,采用GET或者POST提交请求时,getParameter()方法接收到的参数值乱码. 1.乱码产生的原因 请求参数通过浏览器发送给Tomcat服务器,浏览器发送编码,但是tomc ...

  4. .net core 问题:413 Request Entity Too Large nginx

    https://stackoverflow.com/questions/38698350/increase-upload-file-size-in-asp-net-core The other ans ...

  5. RequestMethod用法小结和注意事项

    本文为博主原创,未经允许不得转载: RequestMethod为在@RequestMapping注解中使用的一个属性,用来标识请求的方法类型,可参考@RequestMapping源码: @Target ...

  6. CSS3实现基本图形

    http://blog.csdn.net/laokdidiao/article/details/51189476 代码: <!DOCTYPE html> <html> < ...

  7. 【Python】yield

    彻底理解Python中的yield 2017年04月21日 17:49:57 阅读数:19733 阅读别人的python源码时碰到了这个yield这个关键字,各种搜索终于搞懂了,在此做一下总结: 通常 ...

  8. JavaScript重点知识(二)

    三.JS的API 3.1知识点(DOM) 1)DOM本质 将html结构化成浏览器和JS可识别可操作的东西 2)变量计算---强制类型转换 获取DOM节点 Attribute(对html标签属性的修改 ...

  9. npm install 报错ERR! 404 Not Found: event-stream@3.3.6

    在win下开发的node工程,在linux下用dockerfile部署时,遇到npm install时报错 Step / : RUN npm install ---> Running in 2e ...

  10. java基础题集

    1.什么是java虚拟机?为什么java被称作是“平台无关的编程语言”? java虚拟机是一个可以执行java字节码的虚拟机进程.java源文件被编译成能被java虚拟机执行的字节码文件. java被 ...