Letter Combinations of a Phone Number——LeetCode

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"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

题目大意:给一组数字,按照电话键盘上的组合输出所有的可能。

解题思路:直接递归写DFS,输出所有组合。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LetterCombinationsofaPhoneNumber {

    public static void main(String[] sure) {
        new LetterCombinationsofaPhoneNumber().letterCombinations("213");
    }

    static Map<Character, String> mapping = new HashMap<>();

    static {
        mapping.put(‘0‘, "");
        mapping.put(‘1‘, "");
        mapping.put(‘2‘, "abc");
        mapping.put(‘3‘, "def");
        mapping.put(‘4‘, "ghi");
        mapping.put(‘5‘, "jkl");
        mapping.put(‘6‘, "mno");
        mapping.put(‘7‘, "pqrs");
        mapping.put(‘8‘, "tuv");
        mapping.put(‘9‘, "wxyz");
    }

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return res;
        }
        combine(digits, "", res, digits.length());
        System.out.println(res);
        return res;
    }

    private void combine(String digits, String tmp, List<String> res, int length) {
        if (length == 0) {
            res.add(tmp);
            return;
        }
        for (int i = 0; i < digits.length(); i++) {
            String val = mapping.get(digits.charAt(i));
            if ("".equals(val)) {
                combine(digits.substring(i + 1), tmp, res, length - 1);
            } else {
                for (char c : val.toCharArray()) {
                    combine(digits.substring(i + 1), tmp + c, res, length - 1);
                }
            }
        }
    }

}
时间: 2024-08-02 09:45:22

Letter Combinations of a Phone Number——LeetCode的相关文章

Letter Combinations of a Phone Number leetcode java

题目: 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" Output: ["ad", "ae"

Letter Combinations of a Phone Number -- leetcode

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" Output: ["ad", "ae", &q

Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路

本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了. 同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如果你也查看问题讨论的话会发现有和我一模一样的代码,其实就是我PO的:) 书接正文,基于循环的两种思路如下: 第一种思路 比如"234"这个字符串,我可以先将0...1的所有排列找到-->{"a", "b", "c"} 再进一步

LeetCode: Letter Combinations of a Phone Number [018]

[题目] 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" Output: ["ad", "ae"

【Leetcode长征系列】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" Output: ["ad", "ae"

[LeetCode][JavaScript]Letter Combinations of a Phone Number

Letter Combinations of a Phone Number Total Accepted: 40709 Total Submissions: 157759My Submissions Question Solution Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just l

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" Outp

【leetcode刷题笔记】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" Output: ["ad", "ae", &q

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&qu