solution.md 7.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 电话号码的字母组合
每日一练社区's avatar
每日一练社区 已提交
2
<p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png" style="width: 200px;" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = "23"<strong><br />输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = ""<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = "2"<strong><br />输出:</strong>["a","b","c"]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>0 <= digits.length <= 4</code></li>	<li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>
每日一练社区's avatar
每日一练社区 已提交
3 4
<p><p>给定一个仅包含数字 <code>2-9</code> 的字符串,返回所有它能表示的字母组合。答案可以按 <strong>任意顺序</strong> 返回。</p><p>给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。</p><p><img src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0017.Letter%20Combinations%20of%20a%20Phone%20Number/images/17_telephone_keypad.png" style="width: 200px;" /></p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>digits = "23"<strong><br />输出:</strong>["ad","ae","af","bd","be","bf","cd","ce","cf"]</pre><p><strong>示例 2:</strong></p><pre><strong>输入:</strong>digits = ""<strong><br />输出:</strong>[]</pre><p><strong>示例 3:</strong></p><pre><strong>输入:</strong>digits = "2"<strong><br />输出:</strong>["a","b","c"]</pre><p> </p><p><strong>提示:</strong></p><ul>	<li><code>0 <= digits.length <= 4</code></li>	<li><code>digits[i]</code> 是范围 <code>['2', '9']</code> 的一个数字。</li></ul>
<p>以下错误的选项是?</p></p>
每日一练社区's avatar
每日一练社区 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## aop
### before
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
```cpp
int main()
{
    Solution sol;
    string digits = "23";
    vector<string> res;
    res = sol.letterCombinations(digits);
    for (auto i : res)
        cout << i + ' ';
    return 0;
}
```

## 答案
```cpp
string letterMap[10] = {" ", " ", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
class Solution
{
public:
    void combinStr(const string &digits, size_t index, const string &str, vector<string> &strs)
    {
        if (index == digits.size())
        {
            strs.push_back(str);
            return;
        }
        string letters = letterMap[digits[index] - '0'];
        for (size_t i = 0; i < letters.size(); i++)
        {
            combinStr(digits, index, str + letters[i], strs);
        }
    }
    vector<string> letterCombinations(string digits)
    {
        vector<string> strs;
        if (digits.empty())
            return strs;
        size_t index = 0;
        string str;

        combinStr(digits, index, str, strs);
        return strs;
    }
};
```
## 选项

### A
```cpp
class Solution
{
public:
    vector<string> result;
    vector<string> letterCombinations(string digits)
    {
        string temp;
        if (digits.length() == 0)
            return result;
        getAns(digits, 0, temp, result);
        return result;
    }
    void getAns(string digits, int start, string temp, vector<string> &result)
    {
        if (temp.size() == digits.length())
            result.push_back(temp);
        else
        {
            vector<char> let = getLet(digits[start]);
            for (int i = 0; i < let.size(); i++)
            {
                temp.append(1, let[i]);
                getAns(digits, start + 1, temp, result);
                temp.pop_back();
            }
        }
    }
    vector<char> getLet(char i)
    {
        vector<char> let;
        if (i == '2')
        {
            let.push_back('a');
            let.push_back('b');
            let.push_back('c');
        }
        else if (i == '3')
        {
            let.push_back('d');
            let.push_back('e');
            let.push_back('f');
        }
        else if (i == '4')
        {
            let.push_back('g');
            let.push_back('h');
            let.push_back('i');
        }
        else if (i == '5')
        {
            let.push_back('j');
            let.push_back('k');
            let.push_back('l');
        }
        else if (i == '6')
        {
            let.push_back('m');
            let.push_back('n');
            let.push_back('o');
        }
        else if (i == '7')
        {
            let.push_back('p');
            let.push_back('q');
            let.push_back('r');
            let.push_back('s');
        }
        else if (i == '8')
        {
            let.push_back('t');
            let.push_back('u');
            let.push_back('v');
        }
        else if (i == '9')
        {
            let.push_back('w');
            let.push_back('x');
            let.push_back('y');
            let.push_back('z');
        }
        return let;
    }
};
```

### B
```cpp
class Solution
{
public:
    unordered_map<char, string> map = {{'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"}};
    vector<string> res;
    void backtrack(string &s, int index, string cur)
    {
        if (index == s.size())
        {
            res.push_back(cur);
            return;
        }
        for (int i = 0; i < map[s[index]].size(); ++i)
            backtrack(s, index + 1, cur + map[s[index]][i]);
    }
    vector<string> letterCombinations(string digits)
    {
        if (digits.size() == 0)
            return res;
        string cur;
        backtrack(digits, 0, cur);
        return res;
    }
};
```

### C
```cpp
class Solution
{
public:
    vector<string> letterCombinations(string digits)
    {
        if (digits.size() == 0)
            return {};
        map<char, string> a;
        a.insert(map<char, string>::value_type('2', "abc"));
        a.insert(map<char, string>::value_type('3', "def"));
        a.insert(map<char, string>::value_type('4', "ghi"));
        a.insert(map<char, string>::value_type('5', "jkl"));
        a.insert(map<char, string>::value_type('6', "mno"));
        a.insert(map<char, string>::value_type('7', "pqrs"));
        a.insert(map<char, string>::value_type('8', "tuv"));
        a.insert(map<char, string>::value_type('9', "wxyz"));
        int count = 1;
        for (int i = 0; i < digits.size(); i++)
        {
            count *= a[digits[i]].size();
        }
        vector<string> res(count);
        count = 1;
        for (int i = 0; i < digits.size(); i++)
        {
            int index = 0;
            vector<string> temp(res.begin(), res.begin() + count);
            for (int k = 0; k < count; k++)
            {
                for (auto c : a[digits[i]])
                {
                    res[index] = temp[k] + c;
                    index++;
                }
            }
            count *= a[digits[i]].size();
        }
        return res;
    }
};
```