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]))
        }
    }
}
相关推荐
py有趣4 小时前
力扣热门100题之接雨水
算法·leetcode
不会写DN5 小时前
构建一个抗揍的 Go TCP 聊天服务:异常兜底与防御性编程实践
tcp/ip·golang·php
不会写DN7 小时前
Go中如何跨语言实现传输? - GRPC
开发语言·qt·golang
py有趣8 小时前
力扣热门100题之合并区间
算法·leetcode
派大星~课堂8 小时前
【力扣-138. 随机链表的复制 ✨】Python笔记
python·leetcode·链表
cpp_25018 小时前
P10108 [GESP202312 六级] 闯关游戏
数据结构·c++·算法·动态规划·题解·洛谷·gesp六级
py有趣8 小时前
力扣热门100题之最小覆盖子串
算法·leetcode
北顾笙9809 小时前
day15-数据结构力扣
数据结构·算法·leetcode
人道领域9 小时前
【LeetCode刷题日记:24】两两交换链表
算法·leetcode·链表
北顾笙98010 小时前
day16-数据结构力扣
数据结构·算法·leetcode