Golang | Leetcode Golang题解之第17题电话号码的字母组合

题目:

题解:

Go 复制代码
var phoneMap map[string]string = map[string]string{
    "2": "abc",
    "3": "def",
    "4": "ghi",
    "5": "jkl",
    "6": "mno",
    "7": "pqrs",
    "8": "tuv",
    "9": "wxyz",
}

var combinations []string

func letterCombinations(digits string) []string {
    if len(digits) == 0 {
        return []string{}
    }
    combinations = []string{}
    backtrack(digits, 0, "")
    return combinations
}

func backtrack(digits string, index int, combination string) {
    if index == len(digits) {
        combinations = append(combinations, combination)
    } else {
        digit := string(digits[index])
        letters := phoneMap[digit]
        lettersCount := len(letters)
        for i := 0; i < lettersCount; i++ {
            backtrack(digits, index + 1, combination + string(letters[i]))
        }
    }
}
相关推荐
Pkmer8 小时前
LeetCode 上极少见的工程级滑窗实现
python·leetcode
sheeta19989 小时前
LeetCode 每日一题笔记 日期:2026.05.13 题目:1674. 使数组互补的最少操作次数
笔记·算法·leetcode
YL200404269 小时前
038翻转二叉树
数据结构·leetcode
Liangwei Lin10 小时前
LeetCode 287. 寻找重复数
算法·leetcode·职场和发展
洛水水12 小时前
【力扣100题】38.路径总和 III
算法·leetcode·深度优先
流年如夢13 小时前
二叉树(LeetCode)
数据结构·算法·leetcode·职场和发展
YL2004042614 小时前
035LRU缓存
java·leetcode·缓存
Java面试题总结14 小时前
Go 里什么时候可以“panic”?
开发语言·后端·golang
宵时待雨15 小时前
回溯算法专题1:递归
数据结构·c++·笔记·算法·leetcode·深度优先
洛水水15 小时前
【力扣100题】40.二叉树中的最大路径和
算法·leetcode·深度优先