From d73ef9071538e4f8660d1aa8d62fe815304cf8bf Mon Sep 17 00:00:00 2001 From: X-shuffle <53906918+X-shuffle@users.noreply.github.com> Date: Sat, 19 Jun 2021 21:35:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200017.=E7=94=B5=E8=AF=9D?= =?UTF-8?q?=E5=8F=B7=E7=A0=81=E7=9A=84=E5=AD=97=E6=AF=8D=E7=BB=84=E5=90=88?= =?UTF-8?q?=20go=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0017.电话号码的字母组合 go版本 --- problems/0017.电话号码的字母组合.md | 40 ++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/problems/0017.电话号码的字母组合.md b/problems/0017.电话号码的字母组合.md index aefee698..1562052c 100644 --- a/problems/0017.电话号码的字母组合.md +++ b/problems/0017.电话号码的字母组合.md @@ -342,6 +342,46 @@ class Solution: Go: + +> 主要在于递归中传递下一个数字 + +```go +func letterCombinations(digits string) []string { + lenth:=len(digits) + if lenth==0 ||lenth>4{ + return nil + } + digitsMap:= [10]string{ + "", // 0 + "", // 1 + "abc", // 2 + "def", // 3 + "ghi", // 4 + "jkl", // 5 + "mno", // 6 + "pqrs", // 7 + "tuv", // 8 + "wxyz", // 9 + } + res:=make([]string,0) + recursion("",digits,0,digitsMap,&res) + return res +} +func recursion(tempString ,digits string, Index int,digitsMap [10]string, res *[]string) {//index表示第几个数字 + if len(tempString)==len(digits){//终止条件,字符串长度等于digits的长度 + *res=append(*res,tempString) + return + } + tmpK:=digits[Index]-'0' // 将index指向的数字转为int(确定下一个数字) + letter:=digitsMap[tmpK]// 取数字对应的字符集 + for i:=0;i