solution.md 5.1 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 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
# 电话号码的字母组合
以下错误的选项是?
## 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;
    }
};
```