1. 程式人生 > >LeetCode Letter Combinations of a Phone Number DFS

LeetCode Letter Combinations of a Phone Number DFS

DFS

C++ code:

class Solution {
private:
    vector<string> mapping = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        if(digits.size() == 0) return res;
        int index = 0
; string value = ""; dfs(index, digits, res, value); return res; } void dfs(int index, string digits, vector<string>& res, string& value) { if(index == digits.size()) { res.push_back(value); return; } for(int
i = 0; i < mapping[digits[index] - '2'].size(); ++i) { value.push_back(mapping[digits[index] - '2'][i]); dfs(index + 1, digits, res, value); value.pop_back(); } } };

精簡後:

class Solution {
public:
    const vector<string> dict={"","","abc","def"
,"ghi","jkl","mno","pqrs","tuv","wxyz"}; void dfs(string digits, int depth, string path, vector<string> &res) { if(depth == digits.size()) { res.push_back(path); return; } for(auto c: dict[digits[depth] - '0']) { dfs(digits, depth+1, path+c, res); } } vector<string> letterCombinations(string digits) { vector<string> res; if(digits.size() == 0) return res; dfs(digits, 0, "", res); return res; } };

相關推薦

LeetCode Letter Combinations of a Phone Number DFS

DFS C++ code: class Solution { private: vector<string> mapping = {"abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wx

Leetcode Letter Combinations of a Phone Number DFS

https://leetcode.com/problems/letter-combinations-of-a-phone-number/ 有一個trick 就是如果是輸入""  這個時候返回空 其他就是程式碼寫的簡潔的問題了 #include <cstdio>

leetcode-Letter Combinations of a Phone Number歸納遞迴操作

【原創文章  作者:NicoleHe】 這是一道非常簡單的遞迴呼叫的題,但是我在思考時產生了一些混亂,隱隱約約覺得很熟悉,就是不能用程式碼實現出來。 因此這裡針對這類“依次新增”的問題給出大致的遞迴操作步驟。以後會更新對應的棧操作實現。 首先放出原題:Letter Combinations o

[LeetCode] Letter Combinations of a Phone Number

其實我一開始讀不懂題目。。我以為應該是出來的答案應該都是兩位的,我還特地拷貝AC程式碼執行一下才知道題目意思= =雖然看了懂了題意還是不會做 程式碼挺容易懂的,運用遞迴,以level為digits的遍歷指標,在遍歷每個指標對應的text組成out,總的來說是DF

LeetCode-Letter-Combinations-Of-A-Phone-Number

一、Description 給定一個字串,包含從2 - 9位數,返回所有可能代表的字母組合。一個數字對映的字母的可能(就像在電話裡按鈕)如下所示。請注意,1不對映到任何字母。 Example:

leetcode Letter Combinations of a Phone Number

遞迴方法解決。 /** Return an array of size *returnSize. Note: The returned array must be malloced, assume caller calls free(). / char* le

[LeetCode] 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

17. Letter Combinations of a Phone Number (DFS, String)

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

Leetcode dfs 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

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][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): """

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. 題目要

[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", "

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

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

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