1. 程式人生 > >Letter Combinations of a Phone Number:給定數字求出對應的多個字母的組合可能問題

Letter Combinations of a Phone Number:給定數字求出對應的多個字母的組合可能問題

Given a digit string, 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.

Input:Digit string "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:
Although the above answer is in lexicographical order, your answer could be in any order you want.

解釋:0,1,2,3,4,5,6,7,8,9 這幾個數字分別對應 {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}字母,給定一串數字,求解出所有可能的字母組合。

思路:既然是窮舉出所有可能,當然深度優先遍歷最容易理解。當長度滿足要求時,儲存即可。每種數字對應的字母個數不同,當然要每個對應字母都要考慮到。

 public static final String[] lex = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
   public List<String> letterCombinations(String digits) {        
        List<String> l = new ArrayList();
        if(digits.length()==0) return l;
        dfs(0,digits,"",l);
        return l;
    }  
    public static void dfs(int index,String digits,String prefix,List list){
        if(index>=digits.length()){
            list.add(prefix);
            return;
        }
        for(int i = 0;i<lex[digits.charAt(index)-'0'].length();i++){
            dfs(index+1,digits,prefix+lex[digits.charAt(index)-'0'].charAt(i),list);
        }
    }



相關推薦

Letter Combinations of a Phone Number:給定數字對應字母組合可能問題

Given a digit string, return all possible letter combinations that the number could represent. A m

leetCode 17.Letter Combinations of a Phone Number(電話數字對應字母組合) 解題思路和方法

Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A m

Leetcode 17. Letter Combinations of a Phone number

res bsp self. col join lee num nat leetcode 求給出的數字串,如果按照電話鍵盤的編譯方式,可以給出多少那些對應的數字組合。例如: Input:Digit string "23" Output: ["ad", "ae", "af"

leetcode_017 Letter Combinations of a Phone Number

like present class digits div all dfs hat upload Given a digit string, return all possible letter combinations that the number could repr

17. Letter Combinations of a Phone Number

leetcode lan esc ber des let bsp nat leet https://leetcode.com/problems/letter-combinations-of-a-phone-number/#/description 17. Letter C

Letter Combinations of a Phone Number

elf cal con rep python lis commons wiki san Given a digit string, return all possible letter combinations that the number could represent

[LeetCode][Java] Letter Combinations of a Phone Number

article art present net pub spa line note not 題目: Given a digit string, return all possible letter combinations that the number

LeetCode(17)Letter Combinations of a Phone Number

app code com elf img bject dex logs nbsp 題目如下: Python代碼: class Solution(object): def letterCombinations(self, digits): """

LeetCode17:Letter Combinations of a Phone Number

一道 thumb dig res digits line 每一個 med parent Given a digit string, return all possible letter combinations that the number could

LeetCode:17. Letter Combinations of a Phone Number(Medium)

class stat def es2017 不同的 進行 先進先出 ati tps 1. 原題鏈接 https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/ 2. 題目要

電話號碼的字母組合 · Letter Combinations of a Phone Number

特殊情況 結果 span 畫圖 給定 空間分析 clu tel 問題 [抄題]: Given a digit string excluded 01, return all possible letter combinations that the number could

[leetcode]17. Letter Combinations of a Phone Number手機鍵盤的字母組合

poi clu char evel 個數字 The let alt input Given a string containing digits from 2-9 inclusive, return all possible letter combinations that

【Leetcode】17、Letter Combinations of a Phone Number

question pos dia ppi stage ble you xpl class 題目 Given a string containing digits from 2-9 inclusive, return all possible letter combinat

leetcode 17-Letter Combinations of a Phone Number(medium)

shm put note 下標 維護 nta style == emp Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the

leetcode#17. Letter Combinations of a Phone Number

commons 註意 構建 media ber str http wiki pan 給定一個僅包含數字 2-9 的字符串,返回所有它能表示的字母組合。 給出數字到字母的映射如下(與電話按鍵相同)。註意 1 不對應任何字母。 示例: 輸入:"23" 輸出:["ad", "

leetcode17. Letter Combinations of a Phone Number

python3 class Solution: def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """

LeetCode(17)-Letter Combinations of a Phone Number

17.Letter Combinations of a Phone Number Given a string containing digits from 2-9 inclusive, return all possible letter combinations that

Leetcode -17. Letter Combinations of a Phone Number

這個問題我最開始的想的是使用樹的結構去做,之後用python實現的時候是使用了幾個for迴圈: class Solution: def letterCombinations(self, digits): """ :type digits: str

17. Letter Combinations of a Phone Number--back tracking--字元陣列

17 給出一個電話撥號盤,數字2-9代表一些字母,已知數字,求出所有字母的可能 例子 比如 求"23" , 2--->"abc" 3-->"def" 畫出遞迴數其實非常簡單: 和77. Combinations 本質上是一樣的, 都是求所排列問題,但和數字排列不同的時,每一

LeetCode 17. 電話號碼的字母組合 Letter Combinations of a Phone Number

8-1 樹形問題 Letter Combinations of a Phone Number 題目: LeetCode 17. 電話號碼的字母組合 給定一個僅包含數字 2-9 的字串,返回所有它能表示的字母組合。 給出數字到字母的對映如下(與電話按鍵相同)。注意 1 不對應任