題目:
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent. Return the answer in any order.
A mapping of digits to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
給定一個包含數字的字串,傳回該數字可能代表的所有字母組合。以任意順序2-9回傳答案。
下面給出了數字到字母的映射(就像電話按鈕一樣)。注意,1 不映射到任何字母。
解題思路
這題屬於 回溯 (Backtracking) 的典型題:
建立數字與字母的映射表。
使用回溯遞迴 (DFS):
當目前組合的長度等於輸入字串長度 → 加入答案。
否則,取當前數字的所有字母,逐一加入組合並遞迴下一層。