-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Letter Combinations of a Phone Number
- Loading branch information
Showing
1 changed file
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 存储数字到字符串的映射,然后递归即可。 | ||
|
||
|
c054770
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to following URLs: