/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char *hashMap[10] = {"_", "!@#", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void backtrace(char **result, int *returnSize, char *tmp, int *tmpSize, char *digits, int index)
{
if(*tmpSize == strlen(digits))
{
memcpy(result[(*returnSize)++], tmp, *tmpSize);
return;
}
int i;
for(i = 0; i<strlen(hashMap[(digits[index]-'0')]); i++)
{
tmp[(*tmpSize)++] = hashMap[(digits[index]-'0')][i];
backtrace(result, returnSize, tmp, tmpSize, digits, index+1);
(*tmpSize)--;
}
return;
}
char ** letterCombinations(char * digits, int* returnSize){
*returnSize = 0;
if(strlen(digits) == 0)
{
return NULL;
}
char **result = (char **)malloc(sizeof(char *) * 256);
int i;
for(i = 0; i<256; i++)
{
result[i] = (char *)malloc(sizeof(char) * (strlen(digits)+1));
memset(result[i], 0, sizeof(char) * (strlen(digits)+1));
}
char *tmp = (char *)malloc(sizeof(char) * (strlen(digits) + 1));
memset(tmp, 0, sizeof(char) * (strlen(digits) + 1));
int tmpSize = 0;
backtrace(result, returnSize, tmp, &tmpSize, digits, 0);
free(tmp);
return result;
}
#17. 电话号码的字母组合
最新推荐文章于 2023-08-25 12:20:00 发布