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));
        }

    }
}

原文地址:https://www.cnblogs.com/qinyuguan/p/11317054.html

时间: 2024-07-28 16:17:42

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

电话号码的字母组合 &#183; Letter Combinations of a Phone Number

[抄题]: Given a digit string excluded 01, 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. 给定 "23" 返回 ["ad", "ae", "

Leetcode之回溯法专题-39. 组合总数(Combination Sum)

给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选取. 说明: 所有数字(包括 target)都是正整数. 解集不能包含重复的组合. 示例 1: 输入: candidates = [2,3,6,7], target = 7, 所求解集为: [ [7], [2,2,3] ] 示例 2: 输入: candidates = [2,3,5], target

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

[Leetcode] Backtracking回溯法解题思路

碎碎念: 最近终于开始刷middle的题了,对于我这个小渣渣确实有点难度,经常一两个小时写出一道题来.在开始写的几道题中,发现大神在discuss中用到回溯法(Backtracking)的概率明显增大.感觉如果要顺利的把题刷下去,必须先要把做的几道题题总结一下. 先放上参考的web: https://segmentfault.com/a/1190000006121957 http://summerisgreen.com/blog/2017-07-07-2017-07-07-算法技巧-backtr

回溯法专题

回溯法专题 回溯法(Backtracking)指的是在每个状态的固化,比如f(1)是一个状态,f(2)是另一个状态.从f(1)到f(2),状态改变,各种依赖状态的数据也改变了,那么从f(2)到f(1)的时候,又回到了f(1)的状态了.回溯常常配合深度优先执行,在往深度时候,数据产生变化,然后在递归回来的时候,又回到了之前的状态. 46. Permutations 拿这道全排列来讲,它按照正常顺序列出了全部的排列结果,也可以叫做字典序,主要是输入是按从小到大的话,输出就是字典序. 按照递归的方式来

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, 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. Input:Digit string "23"Ou

17. 电话号码的字母组合

17. 电话号码的字母组合 暴力即可,深搜 or 迭代 class Solution { Map<Character, String[]> map = new HashMap<Character, String[]>(); public List<String> letterCombinations(String digits) { if (null == digits || digits.length() == 0) return new LinkedList<

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, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given

【leetcode】Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Given a digit string, 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. Input:Digit string "23" Outpu