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]))
        }
    }
}
相关推荐
Tony Bai4 小时前
高并发后端:坚守 Go,还是拥抱 Rust?
开发语言·后端·golang·rust
在风中的意志10 小时前
[数据库SQL] [leetcode-584] 584. 寻找用户推荐人
数据库·sql·leetcode
毅炼10 小时前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先
DICOM医学影像13 小时前
15. Go-Ethereum测试Solidity ERC20合约 - Go-Ethereum调用合约方法
开发语言·后端·golang·区块链·智能合约·以太坊·web3.0
我要用代码向我喜欢的女孩表白15 小时前
对象存储路径文件1TB以上文件比对,go语言
ios·golang·xcode
leoufung16 小时前
LeetCode 67. Add Binary:从面试思路到代码细节
算法·leetcode·面试
无限进步_16 小时前
【C语言】循环队列的两种实现:数组与链表的对比分析
c语言·开发语言·数据结构·c++·leetcode·链表·visual studio
linsa_pursuer16 小时前
最长连续序列
java·数据结构·算法·leetcode
POLITE317 小时前
Leetcode 54.螺旋矩阵 JavaScript (Day 8)
javascript·leetcode·矩阵
巴塞罗那的风18 小时前
golang协程泄漏排查实战
开发语言·后端·golang