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]))
        }
    }
}
相关推荐
qq_4523962313 小时前
第五篇:《接口与错误处理:Go 的哲学》
开发语言·后端·golang
程序猿乐锅14 小时前
【数据结构与算法 | 第六篇】力扣1109,1094差分数组
java·算法·leetcode
布朗克16815 小时前
Go 入门到精通-29-测试与基准
开发语言·后端·golang·测试与基准
hold?fish:palm17 小时前
9 找到字符串中所有字母异位词
c++·算法·leetcode
Sw1zzle18 小时前
算法入门(六):贪心算法 - 基础入门(Leetcode 121/455/860/376/738)
算法·leetcode·贪心算法
青山木18 小时前
Hot 100 --- 岛屿数量
java·数据结构·算法·leetcode·深度优先·广度优先
啦啦啦啦啦zzzz19 小时前
算法:回溯算法
c++·算法·leetcode
止语Lab21 小时前
从 PHP 到 Go:真正迁移的是复杂度的归属
android·golang·php
小肝一下21 小时前
3. 单链表
c语言·数据结构·c++·算法·leetcode·链表·dijkstra
tachibana221 小时前
hot100 前 K 个高频元素(347)
java·数据结构·算法·leetcode