题目

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. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)

    1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...

  2. map使用

    // map使用 1 #include <iostream> #include "insertVal.h" #include "sort.h" us ...

  3. JavaScript实现数字时钟功能

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  4. css3的过滤效果

    上面的图片就是css3新特性的滤镜效果,学会了这些那么我们这群爱美爱帅的大web是不是就可以完美的用代码实现照片美化了捏~~ 好,咱们先把照片后面的白框实现, <style> #div1{ ...

  5. 图片放大不失真软件PhotoZoom如何使用?

    PhotoZoom可以将我们一些过于像素低的照片可以无失真放大,那么PhotoZoom是如何实现无失真照片放大的呢? 以上图像中的编号表示每个步骤应操作的位置. 单击“打开”,并选择您想调整大小的图像 ...

  6. 这里介绍两种将矩阵写入TXT文件的方法。

    方法1 fid = fopen('data.txt','wt'); % data.txt为写入文件名 matrix = M; % M为要存储的矩阵 [m,n]=size(matrix); for i= ...

  7. 获取Centos7安装Docker各种姿势(指定版本)(转载)

    Centos7安装docker社区(CE)版 官网指导:https://docs.docker.com/engine/installation/linux/docker-ce/centos/#inst ...

  8. Pycharm 4.5.4 for ubuntu 16.04 下载与安装教程

    首先,我们需要有一台已经安装好 ubuntu 16.04 的操作系统,并且配置好java环境: 方法1: 默认安装 apt-get install default-jdk -y # 安装官网最新的ja ...

  9. centos7 rpm 安装 rabbitMQ 最新版

    首先打开官网: http://www.rabbitmq.com/install-rpm.html 先到右侧导航栏来看一下 : 第一个红框是指的在linux中安装,全英文的,乱的一笔,但是静下心来就可以 ...

  10. python笔记4----字典

    1.哈希: 输入任意长度,输出固定长度. 即判断是否哈希,即判断可不可变. 2.创建字典 (1)直接创建:dic={1:'a',2:'b',3:'c'} (2)dict函数创建: list=[(1,' ...