• LeetCode 17 Java 实现


    1. 题目

    给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。答案可以按 任意顺序 返回。

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

    // 可以前往题目链接查看电话按键图片
    2: abc;     3: def;     4: ghi;     5: jkl;
    6: mno;     7: pqrs;    8: tuv;     9: wxyz;
    
    • 1
    • 2
    • 3
    示例1
    
    输入:digits = "23"
    输出:["ad","ae","af","bd","be","bf","cd","ce","cf"]
    
    示例2
    
    输入:digits = ""
    输出:[]
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2. 题解参考

    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    
    public class LeetCode17_01_02 {
    
        // 定义结果集
        public List<String> ret = new ArrayList<>();
        // 定义每次遍历时的临时结果,考虑到字符串拼接,并且没有并发需求,就使用 StringBuilder
        public StringBuilder stringBuilder = new StringBuilder();
    
    
        public List<String> letterCombinations(String digits) {
            // idea 中调试的时候发现 [""] 也会显示成 []
            // TODO 暂不清楚是什么原因
            if (digits == null || digits.length() == 0) {
                return ret;
            }
    
            // 因为 leetcode中 使用类静态变量总是会冲突,所以就这儿作为局部变量申明了
            // 我觉得更好的方式是初始化类的时候就初始化 NumToLetter
            List[] numToLetter = this.getNumToLetter();
    
            backTracking(numToLetter, digits, 0);
            return ret;
        }
    
        /**
         * 获取一个数字到字母的映射表
         *
         * @return List[]
         */
        public List[] getNumToLetter() {
            // 定义一个数组索引为数字,每个索引存放一个链表的地址,链表中存放数字对应的字母集合
            // 数组长度定义为 10,索引对应数字
            List[] numToLetter = new List[10];
            // 定义一个链表临时存放每个数字对应的字母集合
            List<Character> tempCharList = new LinkedList<>();
            // 定义一个变量,专门记录字母的索引
            int charIndex = 0;
    
            // 不太想手敲 abcd,总感觉不灵活,所以就使用循环添加
            for (int i = 2; i < 10; i++) {
                // 每个数字对应三个字母
                for (int j = 0; j < 3; j++) {
                    tempCharList.add((char) ('a' + charIndex));
                    charIndex++;
                }
                // 7 和 9 对应四个字母,需要单独考虑
                if (i == 7 || i == 9) {
                    tempCharList.add((char) ('a' + charIndex));
                    charIndex++;
                }
                numToLetter[i] = new ArrayList<>(tempCharList);
                tempCharList.clear();
            }
            return numToLetter;
        }
    
        /**
         * @param numToLetter 数字到字母的映射表
         * @param number      表示需要组合的数字
         * @param index       表示此轮递归需要遍历的数字的索引
         */
        public void backTracking(List[] numToLetter, String number, int index) {
            // 当字符串中的数字都读取完后,返回
            if (index == number.length()) {
                ret.add(stringBuilder.toString());
                return;
            }
    
            // '0' 不等于 0 务必相减
            // 否则 java.lang.ArrayIndexOutOfBoundsException: Index 50 out of bounds for length 10
            int num = number.charAt(index) - '0';
            for (int i = 0; i < numToLetter[num].size(); i++) {
                stringBuilder.append(numToLetter[num].get(i));
                backTracking(numToLetter, number, index + 1);
                stringBuilder = stringBuilder.deleteCharAt(stringBuilder.length() - 1);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    3. 解题思路

    Leetcode17解题思路

    4. 代码下载

    1. Github algorithm-learning/LeetCode17_01_02.java at master · cc01cc/algorithm-learning: https://github.com/cc01cc/algorithm-learning/blob/master/practice/leetcode/LeetCode17_01_02.java
    2. (备用,非同步)城通网盘 algorithm-learning/LeetCode17_01_02.java: https://url57.ctfile.com/f/37032957-628657087-10aa46?p=9427 (访问密码: 9427)


    • 本文系个人学习总结,
    • 若代码逻辑等存在不严谨的地方,
    • 若文字表述存在不易理解的地方,欢迎讨论,建议;
    • 关于解题思路,我想尽可能直观的描述,所以选择了图的方式,
    • 希望可以对大家理解提供些许帮助;
    • 代码笔记等存放并更新自
    • 感谢
  • 相关阅读:
    B/B+树索引和哈希索引
    【树莓派】windows和树莓派之间文件共享(Samba)
    机房运维管理软件不知道用哪个好?
    数据结构与算法——排序算法
    Mac里有多个版本的Python,产生的奇奇怪怪的问题
    Code Representation方面的Empirical Studies
    LeetCode高频题79. 单词搜索,如果 word 存在于网格中,返回 true ;否则,返回 false
    pandas创建及读取excel文件
    一本通1058;一元二次方程
    全文检索-Elasticsearch-入门
  • 原文地址:https://blog.csdn.net/m0_49270962/article/details/126064675