Leetcode之回溯法专题-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
【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)的更多相关文章
- [Swift]LeetCode17. 电话号码的字母组合 | Letter Combinations of a Phone Number
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 电话号码的字母组合 · Letter Combinations of a Phone Number
[抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...
- Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III)
Leetcode之回溯法专题-216. 组合总和 III(Combination Sum III) 同类题目: Leetcode之回溯法专题-39. 组合总数(Combination Sum) Lee ...
- Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)
Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...
- Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning)
Leetcode之回溯法专题-131. 分割回文串(Palindrome Partitioning) 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. ...
- Leetcode之回溯法专题-90. 子集 II(Subsets II)
Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...
- Leetcode之回溯法专题-79. 单词搜索(Word Search)
Leetcode之回溯法专题-79. 单词搜索(Word Search) 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元 ...
- Leetcode之回溯法专题-78. 子集(Subsets)
Leetcode之回溯法专题-78. 子集(Subsets) 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: nums = ...
- Leetcode之回溯法专题-77. 组合(Combinations)
Leetcode之回溯法专题-77. 组合(Combinations) 给定两个整数 n 和 k,返回 1 ... n 中所有可能的 k 个数的组合. 示例: 输入: n = 4, k = 2 输 ...
随机推荐
- golang在多个go routine中进行map或者slice操作应该注意的对象。
因为golang的map和列表切片都是引用类型,且非线程安全的,所以在多个go routine中进行读写操作的时候,会产生“map read and map write“的panic错误. 某一些类型 ...
- Jenkins-slave实现并行的自动化测试
前言 上篇文章搭建了Jenkins-slave的分布式测试环境,我一直在想一个问题,使用这种模式能不能实现并发的自动化测试?我的想法是:同一套UI自动化的测试代码,是否能够通过一个Job绑定多个sla ...
- apache httpd多后缀解析漏洞复现
apache httpd多后缀解析漏洞复现 一.漏洞描述 Apache Httpd支持一个文件拥有多个后缀,不同的后缀执行不同的命令,也就是说当我们上传的文件中只要后缀名含有php,该文件就可以被解析 ...
- Linux文件系统损坏导致无法正常启动与fsck修复工具
今天在打开自己的虚拟机学习的时候,发现在文件系统检查过程中出现以下的报错: /dev/mapper/VolGroup-lv_root:UNEXPECTED INCONSISTENCY;RUN fsck ...
- Waiting for 1 instance(s) to be deallocated
看是不是马虎,自己的xampp,也就是mysql有没有打开
- NPM - 检查并更新项目依赖的版本
原文地址:https://acme.top/nodejs-npm-check-updates 前言 经常会遇到 package.json 中的库有更新,但是太多一个一个的来很费事,幸好有个工具 npm ...
- 【Java】You have an error in your SQL syntax ...
详情如下: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server v ...
- C# 二维码的生成
nuget 搜索qrcodenet,然后选择下载gma.qrcodenet public partial class Form1 : Form { public Form1() { Initializ ...
- 泥瓦匠 5 年 Java 的成长感悟(下)
继续<泥瓦匠 5 年 Java 的成长感悟(上)>,大致包括下面几点: 学技术的心态 学技术的学法 工作的心态 工作的硬技能 工作的软实力 听点雷子的民谣,我就安静地感概感概.上次说写的, ...
- Spring的依赖注入和管理Bean
采用Spring管理Bean和依赖注入 1.实例化spring容器 和 从容器获取Bean对象 实例化Spring容器常用的两种方式: 方法一: 在类路径下寻找配置文件来实例化容器 [推荐使用] Ap ...