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

思路

使用回溯算法

code

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)
	}
}

猜你喜欢

转载自blog.csdn.net/anakinsun/article/details/88867623
今日推荐