45 lines
847 B
Go
45 lines
847 B
Go
package top100liked
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
// https://leetcode.cn/problems/letter-combinations-of-a-phone-number?envType=study-plan-v2&envId=top-100-liked
|
|
|
|
var m = map[byte][]byte{
|
|
'2': []byte("abc"),
|
|
'3': []byte("def"),
|
|
'4': []byte("ghi"),
|
|
'5': []byte("jkl"),
|
|
'6': []byte("mno"),
|
|
'7': []byte("pqrs"),
|
|
'8': []byte("tuv"),
|
|
'9': []byte("wxyz"),
|
|
}
|
|
|
|
func letterCombinations(digits string) []string {
|
|
res := []string{}
|
|
|
|
var helper func(idx int, r []byte)
|
|
helper = func(idx int, r []byte) {
|
|
if idx == len(digits) {
|
|
res = append(res, string(r))
|
|
return
|
|
}
|
|
for j := range m[digits[idx]] {
|
|
r = append(r, m[digits[idx]][j])
|
|
helper(idx+1, r)
|
|
r = r[:len(r)-1]
|
|
}
|
|
}
|
|
|
|
helper(0, []byte{})
|
|
return res
|
|
}
|
|
|
|
func Test(t *testing.T) {
|
|
fmt.Printf("%+v", letterCombinations("23"))
|
|
fmt.Printf("%+v", letterCombinations("2"))
|
|
}
|