トピック:
2〜9の数字のみを含む文字列を指定すると、それが表すことができるすべての文字の組み合わせを返します。回答は任意の順序で返すことができます。
数字から文字へのマッピングは次のようになります(電話ボタンと同じ)。1はどの文字にも対応しないことに注意してください。
例:
**入力:** Digits = "23"
出力: ["ad"、 "ae"、 "af"、 "bd"、 "be"、 "bf"、 "cd"、 "ce"、 "cf"]
解決
コード
class Solution {
private:
const string letterMap[10] = {
"", // 0
"", // 1
"abc", // 2
"def", // 3
"ghi", // 4
"jkl", // 5
"mno", // 6
"pqrs", // 7
"tuv", // 8
"wxyz", // 9
};
public:
vector<string> result;
void getCombinations(const string& digits, int index, const string& s) {
if (index == digits.size()) {
result.push_back(s);
return;
}
int digit = digits[index] - '0';
string letters = letterMap[digit];
for (int i = 0; i < letters.size(); i++) {
getCombinations(digits, index + 1, s + letters[i]);
}
}
vector<string> letterCombinations(string digits) {
result.clear();
if (digits.size() == 0) {
return result;
}
getCombinations(digits, 0, "");
return result;
}
};