Leetcode Golang 17. Letter Combinations of a Phone Number.go

2019-04-12 11:17:01 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412892

思路

使用回溯算法

code

代码语言:javascript复制
func letterCombinations(digits string) []string {
	table := []string{"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}
	ret := []string{}
	if len(digits) > 0 {
		help(&ret, digits, "", 0, table)
	}
	return ret
}

func help(ret *[]string, digits string, cur string, index int, table []string) {
	if index == len(digits) {
		*ret = append(*ret, cur)
		return
	}
	tmp := table[digits[index]-48]
	for _, t := range tmp {
		help(ret, digits, cur string(t), index 1, table)
	}
}

0 人点赞