Letter Combinations of a Phone Number

每日算法——leetcode系列


問題 3Sum Closest

Difficulty: Mediumgit

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.app

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        
    }
};

翻譯

電話號碼的字母組合

難度係數:中等svg

給定一個數字組成的字符串, 返回這個數字全部全部可能表明的字母組合。
每一個數字表明的字母(像電話按鍵)以下面圖顯示同樣spa

輸入:數字字符串 "23"
輸出: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].翻譯

注意:
雖然上面的的答案是按字典順序的,你的答案能夠是你想要的任意順序。code

思路

這題很像 《編程珠璣》 2.6習題及 《編程之美》 3.2節講的東西
這題不用找出單詞, 只是返回字母組合, 相對要簡單。ip

  • map表

這題還有一個map字眼,能夠把按鍵對應的數字組成以下的一個二維數組的map表phoneNum。

0   {' ', '\0', '\0', '\0'}
     1   {'\0', '\0', '\0', '\0'}
     2   {'a', 'b', 'c', '\0'}
     3   {'d', 'e', 'f', '\0'}
     4   {'g', 'h', 'i', '\0'}
     5   {'j', 'k', 'l', '\0'}
     6   {'m', 'n', 'o', '\0'}
     7   {'p', 'q', 'r', 's'} 
     8   {'t', 'u', 'v', '\0'}
     9   {'w', 'x', 'y', 'z'}
  • 輸入的字符串根據map表取出按鍵上的字符

遍歷每一個輸入的數字字符串中的每一個數字(固然每次遍歷的時候要判斷這個是否爲數字isdigit),根據map表取出相應按鍵上的字符依次插入到result中

  • 組合

再循環執行第二步,但要組合。

例如

輸入爲「2」的時候, result = {"a", "b", "c"}
輸入爲「23」的時候:
第二步中第一次 result = {"a", "b", "c"}
第二次 phoneNum3]= {"d", "e", "f"}, result[i] + phoneNum[3就好

代碼

class Solution {
public:
    vector<string> letterCombinations(string digits) {
        
        vector<string> result;
        if (digits.empty()) {
            return result;
        }
        
        // 電話號碼字母map表
        char phoneNum[10][4] = {
            {' ', '\0', '\0', '\0'},
            {'\0', '\0', '\0', '\0'},
            {'a', 'b', 'c', '\0'},
            {'d', 'e', 'f', '\0'},
            {'g', 'h', 'i', '\0'},
            {'j', 'k', 'l', '\0'},
            {'m', 'n', 'o', '\0'},
            {'p', 'q', 'r', 's'},
            {'t', 'u', 'v', '\0'},
            {'w', 'x', 'y', 'z'},
        };
        
        
        for (size_t i = 0; i < digits.size(); ++i) {
            if (!isdigit(digits[i])) {
                vector<string> temp;
                return temp;
            }
            int digit = digits[i] - '0';
            if (result.empty()) {
                for (int j = 0; j < 4; ++j) {
                    if (phoneNum[digit][j] == '\0'){
                        break;
                    }
                    
                    // 數字鍵對應的字符
                    string s;
                    s = phoneNum[digit][j];
                    result.push_back(s);
                }
                continue;
            }
            vector<string> tmpResult;
            for (int j = 0; j < result.size(); ++j) {
                for (int k = 0; k < 4; ++k) {
                    if (phoneNum[digit][k] == '\0'){
                        break;
                    }
                    string s = result[j] + phoneNum[digit][k];
                    tmpResult.push_back(s);
                }
            }
            result = tmpResult;
        }
        return result;
    }
};
相關文章
相關標籤/搜索