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]))
        }
    }
}
相关推荐
Dream it possible!16 小时前
LeetCode 面试经典 150_二叉树_二叉树中的最大路径和(77_124_C++_困难)(DFS)
c++·leetcode·面试·二叉树
做怪小疯子21 小时前
LeetCode 热题 100——子串——和为 K 的子数组
算法·leetcode·职场和发展
希望有朝一日能如愿以偿1 天前
力扣每日一题:仅含1的子串数
算法·leetcode·职场和发展
q***71081 天前
【Golang】——Gin 框架中的表单处理与数据绑定
microsoft·golang·gin
苏小瀚1 天前
算法---FloodFill算法和记忆化搜索算法
数据结构·算法·leetcode
少许极端1 天前
算法奇妙屋(十二)-优先级队列(堆)
数据结构·算法·leetcode·优先级队列··图解算法
Kuo-Teng1 天前
LeetCode 118: Pascal‘s Triangle
java·算法·leetcode·职场和发展·动态规划
q***72871 天前
Golang 构建学习
开发语言·学习·golang
野蛮人6号1 天前
力扣热题100道之207课程表
算法·leetcode·职场和发展
学学学无无止境1 天前
力扣-买卖股票的最佳时机
leetcode