1. 程式人生 > >【演算法設計與分析作業題】第九周:17. Letter Combinations of a Phone Number

【演算法設計與分析作業題】第九周:17. Letter Combinations of a Phone Number

題目

C++ solution

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        vector<string> result;
        if(digits.size() == 0) // 數字串為空串則直接返回空陣列
            return result;
            
        vector<string> letters = { "abc", "def",
        "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz" };
        
        string comb; // 字母組合串
        backTracking(result, letters, digits, 0, comb);
        
        return result;
    }
    
    void backTracking(vector<string>& result, vector<string>& letters,
        string& digits, int index, string& comb)
    {
        if(index == digits.length())  // 搜尋到最後一個字母,得到其中一個字母組合串
        {
            result.push_back(comb);
            return;
        }
            
        for(int i = 0; i < letters[digits[index] - '2'].length(); i++)  // 每一層搜尋該按鍵的字母個數
        {
            comb += letters[digits[index] - '2'][i];  // 將本層按鍵的一個字母加到字母組合串
            backTracking(result, letters, digits, index + 1, comb);  // 深度優先搜尋,搜尋下一層
            comb.pop_back();  // 回溯,去搜索其他分支
        }
    }
};

簡要題解

本題只需採用深度優先搜尋,當搜尋到深度最深時便得到一條路徑,將路徑上的字母合併在一起便得到其中一個字母組合串。然後回溯到上層節點,去搜索其他分支,當所有路徑都搜尋完畢時,就得到全部的字母組合串。簡要示意圖如下:

相關推薦

演算法設計分析作業題17. Letter Combinations of a Phone Number

題目 C++ solution class Solution { public: vector<string> letterCombinations(string digits) { vector<string>

演算法設計分析作業題十週19. Remove Nth Node From End of List

題目 C++ solution /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListN

演算法設計分析作業題十一週20. Valid Parentheses

題目 C++ solution class Solution { public: bool isValid(string s) { stack<char> cstack; for (int i = 0; i < s.si

演算法設計分析作業題第二週1. Two Sum

題目 C++ solution class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { ve

LeetCode17. Letter Combinations of a Phone Number - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a string containing digits from 2-9 inclusive, return all possible

演算法練習--LeetCode--17. Letter Combinations of a Phone Number

Letter Combinations of a Phone NumberMedium Given a string containing digits from 2-9 inclusive, return all possible letter combinatio

演算法設計資料結構三分法求單峰函式的極值

介紹 三分法的思路與二分法很類似,不過其用途沒有那麼廣泛,主要用於求單峰函式的極值。 示例程式碼 void Solve() { double left, right, m1, m2,

LeetCode & 劍指offer刷題回溯法暴力列舉法題5Letter Combinations of a Phone Number

【LeetCode & 劍指offer 刷題筆記】目錄(持續更新中...) Letter Combinations of a Phone Number Given a string containing digits from  

LeetCode-面試演算法經典-Java實現017-Letter Combinations of a Phone Number (電話號碼上的單詞組合)

原題   Given a digit string, return all possible letter combinations that the number could rep

Leetcode17Letter 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

leetcode17.(Medium) Letter Combinations of a Phone Number

題目連結 解題思路: 回溯 提交程式碼: class Solution { public List<String> letterCombinations(String digits) { List<String> res=new

演算法35--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

演算法-Letter Combinations of a Phone Number

  package recruit_and_backstracking class LetterCombinationsOfAPhoneNumber { class Solution { var res: MutableList<String> = A

leetcode17題——**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 li

演算法設計分析6、最大欄位和

/** * 書本:《演算法分析與設計》 * 功能:若給定n個整陣列成的序列a1, a2, a3, ......an, 求該序列形如ai+a(i+1)+......+an的最大值 * 檔案:MaxSum.cpp * 時間:2014年11月30日17:37:26 * 作者:cu

歸併排序和快速排序比較演算法設計分析實驗報告

       下面的原始碼是修改的了時間差精確到了納秒級別的了,但是還是感覺很有誤差。無論怎麼測,總是快排比歸併快,即使是測試資料的陣列長度在10以內。         前面一樣的程式寫的是時間精確到微秒級的,陣列長度大概在一萬以內的,就是歸併排序快了,大於這個長度的快速排

演算法設計分析貪心策略——最佳郵局設定問題

//總是感覺生活很空虛,就只能寫寫部落格看看書上上課這樣子。想出去,去一個遙遠的地方。先來看一下題目:有n戶人家坐落在從西向東的一條街上。從街西頭向東數,第i戶的房子與街西頭的距離是H[i]米,(1≤i≤n), H[1]< H[2] < H[3] … < H

演算法設計資料結構為何程式設計師喜歡將INF設定為0x3f3f3f3f?

在演算法競賽中,我們常常需要用到一個“無窮大”的值,對於我來說,大多數時間我會根據具體問題取一個99999999之類的數(顯得很不專業啊!) 在網上看別人程式碼的時候,經常會看到他們把INF設為0x7fffffff,奇怪為什麼設一個這麼奇怪的十六進位制數,一查

leetcode17Letter Combinations of a Phone Number佇列

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the

演算法設計分析基礎三版習題1.1 4

演算法設計與分析基礎 習題1.1 4 設計一個[√n]的演算法,n是任意正整數。除了賦值和比較運算,該演算法只能用到基本的四則運算。 程式碼實現: #include "iostream" using namespace std; double n; doubl