【Leetcode】17. 电话号码的字母组合(Letter Combinations of a Phone Number)

题目描述:

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。

给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。

示例:

输入:"23"
输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

分析:

用递归就可以完成,但官方把他归进了回溯法,标题就写回溯法吧~

题目中要求给定包含2-9数字的长度为N的字符串,每个数字对应几个字母,求这些所有字母的组合,如图所示。

我们可以用Map的key存储数字2-9,用value存储这个数字对应的字母(例如,2对应abc)。

递归可以很好的解决这个问题,首先从字符串的index=0开始进入,根据Map上该key对应的value值(例如key=2,value="abc"),分别取这些字母,并进入到下一个过程中。

用Map存储的过程如下:

 Map<String, String> map = new HashMap<String, String>() {{
put("2", "abc");
put("3", "def");
put("4", "ghi");
put("5", "jkl");
put("6", "mno");
put("7", "pqrs");
put("8", "tuv");
put("9", "wxyz");
}};

可以看到,答案需要一个List<String>作为返回,我们则可以:

List<String> ans = new ArrayList<>();

接下来到了写函数,我们创建一个名为dfs的函数:

public void dfs(String digits, int step, String answer) {
if (step == digits.length()) {
ans.add(answer);
return;
} char c = digits.charAt(step);
String value = map.get(c +"");
for (int i = 0; i < value.length(); i++) {
dfs(digits, step + 1, answer + value.charAt(i));
}
}

整合一下,最后AC代码为:

class Solution {
List<String> ans = new ArrayList<>();
Map<String, String> map = new HashMap<String, String>() {{
put("2", "abc");
put("3", "def");
put("4", "ghi");
put("5", "jkl");
put("6", "mno");
put("7", "pqrs");
put("8", "tuv");
put("9", "wxyz");
}};
public List<String> letterCombinations(String digits) {
if (digits.length() == 0 || digits == null)
return ans;
dfs(digits, 0, "");
return ans;
} public void dfs(String digits, int step, String answer) {
if (step == digits.length()) {
ans.add(answer);
return;
} char c = digits.charAt(step);
String value = map.get(c + "");
for (int i = 0; i < value.length(); i++) {
dfs(digits, step + 1, answer + value.charAt(i));
} }
}

Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)的更多相关文章

  1. [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number

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

  2. 电话号码的字母组合 · Letter Combinations of a Phone Number

    [抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...

  3. Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)

    Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...

  4. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

  5. Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)

    Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...

  6. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

  7. Leetcode之回溯法专题-79. 单词搜索(Word Search)

    Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...

  8. Leetcode之回溯法专题-78. 子集(Subsets)

    Leetcode之回溯法专题-78. 子集(Subsets) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = ...

  9. Leetcode之回溯法专题-77. 组合(Combinations)

    Leetcode之回溯法专题-77. 组合(Combinations)   给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...

随机推荐

  1. Java--重载与重写的区别

    1.重写必须继承,重载不用.2.重写的方法名,参数数目相同,参数类型兼容,重载的方法名相同,参数列表不同.3.重写的方法修饰符大于等于父类的方法,重载和修饰符无关.4.重写不可以抛出父类没有抛出的一般 ...

  2. java练习---1

    //程序员:罗元昊 2017.9.6public class Ap{ public static void main(String[] args){ System.out.println(" ...

  3. JavaScript基础学习第六天

    目标: 能够使用对象的方式处理数据 ☞ 代码预解析: 1. 变量提升 :当程序中遇到定义变量后,就会将该变量的定义提升到当前作用域的开始位置,不包括变量的赋值 2. 函数提升:当程序中遇到函数的声明时 ...

  4. 【iOS】PLA 3.3.12

    发件人 Apple Program License Agreement PLA We found that your app uses the Advertising Identifier but d ...

  5. 常用GDB命令行调试命令

    po po是print-object的简写,可用来打印所有NSObject对象.使用举例如下: (gdb) po self <LauncherViewController: 0x552c570& ...

  6. 以太坊solidity智能合约-生成随机数

    Solidity随机数生成 在以太坊的只能合约中,没有提供像其他面向对象编程一样的生成随机数的工具类或方法.其实,所谓的随机数也是伪随机的,没有哪一种语言能够真正的生成随机数. 对于solidity来 ...

  7. git_stats安装及使用

    git_stats是仓库代码统计工具,今天我们要求用git_stats工具做项目的代码统计,也是一步一坑的找到了一些方法,在这里记录一下 一.安装 git_stats可以在windows和linux使 ...

  8. Git 服务使用搭建集合

    Git 服务使用搭建集合 一.本地Git 仓库搭建与使用 1.Git 概念介绍 版本控制系统 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统.大部分时候我们使用最频繁的还是对源 ...

  9. Android 9.0 关机流程分析

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  10. Windows上的Linux容器

    翻译自:https://docs.microsoft.com/en-us/virtualization/windowscontainers/deploy-containers/linux-contai ...