我们可以使用递归来解决这个问题。这个想法是逐个考虑每个输入数字,用移动键盘中的每个字符替换该数字,并为下一个数字重复出现。处理完所有数字后,我们打印结果。
// Recursive function to find all possible combinations by replacing key's digits
// with characters of the corresponding list
void findCombinations(auto const &keypad, auto const &input,
string res, int index)
{
// if we have processed every digit of key, print result
if (index == -1) {
cout << res << " ";
return;
}
// stores current digit
int digit = input[index];
// size of the list corresponding to current digit
int len = keypad[digit].size();
// one by one replace the digit with each character in the
// corresponding list and recur for next digit
for (int i = 0; i < len; i++) {
findCombinations(keypad, input, keypad[digit][i] + res, index - 1);
}
}
// main function
int main()
{
vector<char> keypad[] =
{
{}, {}, // 0 and 1 digit don't have any characters associated
{ 'A', 'B', 'C' },
{ 'D', 'E', 'F' },
{ 'G', 'H', 'I' },
{ 'J', 'K', 'L' },
{ 'M', 'N', 'O' },
{ 'P', 'Q', 'R', 'S'},
{ 'T', 'U', 'V' },
{ 'W', 'X', 'Y', 'Z'}
};
int input[] = { 2, 2, 3, 3};
int n = sizeof(input)/sizeof(input[0]);
findCombinations(keypad, input, string(""), n - 1);
return 0;
}