LeetCode算法笔记–Day32
17. Letter Combinations of a Phone Number
题目:
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
example
input: “23”
output: [“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”].
My Answer:
定义函数 backtrack(combination, nextdigit),当nextdigit 非空时,对于 nextdigit[0] 中的每一个字母 letter,执行回溯 backtrack(combination + letter,nextdigit[1:],直至 nextdigit 为空。最后将 combination 加入到结果中。
1 | /** |