Lc17-电话号码的字母组合

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/*
 * 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.

Example:

Input: "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.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 */
public class Lc17 {
    public static List<String> letterCombinations(String digits) {
        // 避免频繁扩容
        LinkedList<String> ans = new LinkedList<String>();
        String[] mapping = new String[] { "0", "1", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };

        // 空判断
        if (digits.length() == 0) {
            return new ArrayList<>();
        }
        // 这里是利用队列的特性进行bfs遍历
        ans.add("");
        // 由于不知道具体由多少个数字,所以用for控制
        for (int i = 0; i < digits.length(); i++) {
            // 找到对应数字 ,比如输入23 ,找到2或者3
            int x = Character.getNumericValue(digits.charAt(i));
            /*
             * 当第0次循环时,队列中的元素长度时空 即0位
             * 当第一次循环时,队列中元素长度时a/b/c,即1位。以此类推
             */
            while (ans.peek().length() == i) {
                // 获取队列中的元素,并移除该元素重新组合
                String t = ans.remove();
                for (Character ch : mapping[x].toCharArray()) {
                    ans.add(t + ch);
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        String digits = "23";
        System.out.println(letterCombinations(digits));
    }
}

原文地址:https://www.cnblogs.com/xiaoshahai/p/12182608.html

时间: 2024-08-30 06:44:07

Lc17-电话号码的字母组合的相关文章

lintcode 中等题:Letter Combinations of a Phone Number 电话号码的字母组合

题目 电话号码的字母组合 给一个数字字符串,每个数字代表一个字母,请返回其所有可能的字母组合. 下图的手机按键图,就表示了每个数字可以代表的字母. 样例 给定 "23" 返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 注意 以上的答案是按照词

[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):电话号码的字母组合

Medium! 题目描述: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

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题电话号码的字母组合

/*17. 电话号码的字母组合 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 说明:

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

Leetcode_17【电话号码的字母组合】

文章目录: 题目 脚本一 脚本一逻辑 题目: 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf&

LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)

翻译 给定一个数字字符串,返回所有这些数字可以表示的字母组合. 一个数字到字母的映射(就像电话按钮)如下图所示. 输入:数字字符串"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"] 备注:尽管以上答案是无序的,如果你想的话你的答案可以是

电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 思路是回溯+递归,利用一个栈

leecode 17. 电话号码的字母组合

给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:"23" 输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 说明:尽管上面的答案是按字典