题目

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.

思路

思路一:递归法

首先建立电话按钮上数字与字母的对应关系,很容易就想到了利用C++里map

思路二:回溯法

Tips

递归法

回溯法

C++

  • 思路一
    map<int,string> table={{2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}};

    vector<string> letterCombinations(string digits) {

        vector<string> result;

        if(digits.length()==0)
return result; combineNum(result,0,"",digits); return result;
} void combineNum(vector<string>& r,int count,const string& a,const string& b){ if (count == b.size()){
r.push_back(a);
return;
} string curStr = table[b[count] - '0']; for (char s : curStr){
combineNum(r, count + 1, a + s, b);
}
}
  • 思路二

python

17. Letter Combinations of a Phone Number[M]电话号码的字母组合的更多相关文章

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

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

  2. LeetCode 17 Letter Combinations of a Phone Number (电话号码字符组合)

    题目链接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/?tab=Description HashMap< ...

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

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

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

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

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

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

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

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

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

  8. 17. Letter Combinations of a Phone Number

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

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

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

随机推荐

  1. 第7章 性能和可靠性模式 Server Clustering(服务器群集)

    上下文 您正在设计要部署应用程序的基础结构层.运行要求包括无法满足的可用性或性能能力,因为基础结构中存在性能瓶颈或故障单点. 影响因素 设计基础结构时,请考虑下列影响因素: 用户希望在使用应用程序时这 ...

  2. BZOJ1211: [HNOI2004]树的计数(prufer序列)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2987  Solved: 1111[Submit][Status][Discuss] Descript ...

  3. 几个概念:x86、x86-64和IA-32、IA-64

    最近在学习操作系统方面的知识,学习操作系统难免要和CPU打交道,虽然现在CPU和操作系统不像计算机发展初期一样是绑定在一起的,但是大家都知道操作系统和CPU Architecture的联系是很紧密的, ...

  4. RAID5存储上parted进行分期及UUID对应关系

    #parted [设备] [命令 [参数]]命令功能: 新增分区:mkpart [primary|logical|extended] [ext3|vfat] 开始 结束 分区表:print 删除分区: ...

  5. Shiro从数据表中初始化资源和权限

    之前在测试的Web工程下,我们在applicationContext.xml中配置了shiroFilter资源拦截器信息: <bean id="shiroFilter" cl ...

  6. Kafka学习笔记(2)----Kafka的架构

    1. 架构图 一个Kafka集群中包含若干个Broker(消息实例),Kafka支持Broker横向扩展,Broker越多,吞吐量越大,同时也包含了若干个Producer(可以是web前端产生的Pag ...

  7. Webstorm 破解2017.1 for Mac

    废话不多说,改了去年分享2016版本的文章,给同学们带来2017.1版本的Mac版本.(win版本网上很多,我这里就不贴出来了). 1.去官仿下载最新的版本  https://www.jetbrain ...

  8. new String(getBytes(ISO-8859-1),UTF-8)中文编码避免乱码

    byte[] b_gbk = "深".getBytes("GBK"); byte[] b_utf8 = "深".getBytes(" ...

  9. python学习小结

    1学习的课程名字叫什么 python:前端初识html,后台基础flask 2怎么输出一句话,用代码举例 输出用:print 例如: 3使用终端工具怎么运行 Python代码 例如我要运行题目2的程序 ...

  10. vj线段树专题

    vj线段树专题题解 单点更新模板 void build(int x,int l,int r){//sum[x]控制l-r区域 if(l==r){Sum[x]=num[l];return ;} int ...