Skip to content

Commit

Permalink
feat: Letter Combinations of a Phone Number
Browse files Browse the repository at this point in the history
  • Loading branch information
leverz committed Dec 17, 2019
1 parent 2b57301 commit c054770
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions content/blog/algorithm/17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: 17. 电话号码的字母组合
date: '2019-12-17T22:36:00.000Z'
---

[https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/](https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/)

使用语言:Go

```Go
func letterCombinations(digits string) []string {
    m := make(map[string][]string)
    m["1"] = []string{}
    m["2"] = []string{"a""b""c"}
    m["3"] = []string{"d""e""f"}
    m["4"] = []string{"g""h""i"}
    m["5"] = []string{"j""k""l"}
    m["6"] = []string{"m""n""o"}
    m["7"] = []string{"p""q""r""s"}
    m["8"] = []string{"t""u""v"}
    m["9"] = []string{"w""x""y""z"}


    return _letterCombinations(digits, m)
}


func _letterCombinations(digits string, m map[string][]string) (res []string) {
    if len(digits) == 0 {
        return
    }
    if len(digits) == 1 {
        return m[string(digits[0])]
    }
    subRes := _letaterCombinations(digits[1:], m)
    for _, item := range m[string(digits[0])] {
        for _, r := range subRes {
            res = append(res, item + r)
        }
    }
    return
}
```

题目很简单,构造一个 map 存储数字到字符串的映射,然后递归即可。


1 comment on commit c054770

@vercel
Copy link

@vercel vercel bot commented on c054770 Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.