鍍金池/ 教程/ Java/ Letter Combinations of a Phone Number(電話號碼的字母組合)
ZigZag Conversion(Z型轉(zhuǎn)換)
String to Integer (atoi)(轉(zhuǎn)換到整型)
Generate Parentheses(生成括號)
Longest Common Prefix(最長公共前綴)
Remove Duplicates from Sorted Array(從已排序數(shù)組中移除重復(fù)元素)
Swap Nodes in Pairs(交換序列中的結(jié)點(diǎn))
Valid Parentheses(有效的括號)
4 Sum(4 個(gè)數(shù)的和)
3 Sum(3 個(gè)數(shù)的和)
3 Sum Closest(最接近的 3 個(gè)數(shù)的和)
Reverse Nodes in k-Group(在K組鏈表中反轉(zhuǎn)結(jié)點(diǎn))
Regular Expression Matching (正則表達(dá)式匹配)
Two Sum
Container With Most Water(最大水容器)
Merge Two Sorted Lists(合并兩個(gè)已排序的數(shù)組)
Remove Nth Node From End of List(從列表尾部刪除第 N 個(gè)結(jié)點(diǎn))
String to Integer (atoi)(轉(zhuǎn)換到整型)
Palindrome Number(回文數(shù))
Longest Substring Without Repeating Characters
Roman to Integer(羅馬數(shù)到整型數(shù))
Integer to Roman(整型數(shù)到羅馬數(shù))
Reverse Integer(翻轉(zhuǎn)整數(shù))
Merge k Sorted Lists(合并 K 個(gè)已排序鏈表)
Implement strStr()(實(shí)現(xiàn) strStr() 函數(shù))
Longest Palindromic Substring(最大回文子字符串)
Add Two Numbers
Letter Combinations of a Phone Number(電話號碼的字母組合)

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

翻譯

給定一個(gè)數(shù)字字符串,返回所有這些數(shù)字可以表示的字母組合。

一個(gè)數(shù)字到字母的映射(就像電話按鈕)如下圖所示。

輸入:數(shù)字字符串“23”
輸出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

備注:盡管以上答案是無序的,如果你想的話你的答案可以是有序的。

原圖

http://wiki.jikexueyuan.com/project/leetcode-book/images/5.png" alt="" />

原文

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.

代碼

看樣子我還是用 C# 順手點(diǎn),一氣呵成……主要是用遞歸,因?yàn)椴恢?digits 的長度到底是多少,不可能寫無數(shù)個(gè) foreach 去判斷。

public class Solution
{
    IList<string> list = new List<string>();
    public Solution()
    {
        list.Insert(0, "abc");
        list.Insert(1, "def");
        list.Insert(2, "ghi");
        list.Insert(3, "jkl");
        list.Insert(4, "mno");
        list.Insert(5, "pqrs");
        list.Insert(6, "tuv");
        list.Insert(7, "wxyz");
    }
    public IList<string> LetterCombinations(string digits)
    {
        IList<string> result = new List<string>();
        if (digits.Length == 0)
            return result;
        if (digits.Length == 1)
        {
            foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2))
            {
                result.Insert(0, a.ToString());
            }
        }
        int count = 0;
        IList<string> temp = LetterCombinations(digits.Substring(1, digits.Length - 1));
        foreach (var a in list.ElementAt(int.Parse(digits[0].ToString()) - 2))
        {
            foreach (var rest in temp)
            {
                result.Insert(count++, a.ToString() + rest);
            }
        }
        return result;
    }
}

以下是復(fù)制來的一段 C++ 代碼,坦白地說,我寫不出來這樣的 C++ 代碼……

class Solution {
public:
    vector<string> letterCombinations(string digits) {        
        vector<string> ans;
        if(digits.size() == 0)
            return ans;

        int depth = digits.size();
        string tmp(depth, 0);
        dfs(tmp, 0, depth, ans, digits);
        return ans;
    }

    void dfs(string &tmp, int curdep, int depth, vector<string> &ans, string &digits){
        if(curdep >= depth){
            ans.push_back(tmp);
            return ;
        }
        for(int i = 0; i < dic[digits[curdep] - '0'].size(); ++ i){
            tmp[curdep] = dic[digits[curdep] - '0'][i];
            dfs(tmp, curdep + 1, depth, ans, digits);
        }
        return ;
    }
private:
string dic[10] = {{""},{""},{"abc"},{"def"},{"ghi"},{"jkl"},{"mno"},{"pqrs"},{"tuv"},{"wxyz"}};
};