美文网首页leetcode
17. Letter Combinations of a Pho

17. Letter Combinations of a Pho

作者: AnakinSun | 来源:发表于2019-03-22 13:24 被阅读0次

    使用回溯算法

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

    相关文章

      网友评论

        本文标题:17. Letter Combinations of a Pho

        本文链接:https://www.haomeiwen.com/subject/fjblvqtx.html