Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C - 17. Letter Combinations of a Phone Number
參考資訊:
https://www.cnblogs.com/grandyang/p/4452220.html
題目:

解答:
int dfs(const char *digits, int pos, int *cidx, char *cbuf, int *ridx, char **rbuf)
{
int i = 0;
const char *BTN[] = {
"abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"
};
if (pos >= strlen(digits)) {
strcpy(rbuf[(*ridx)++], cbuf);
*cidx -= 1;
cbuf[*cidx] = 0;
return 0;
}
const char *t = BTN[digits[pos] - '2'];
int len = strlen(t);
for (i = 0; i < len; i++) {
cbuf[(*cidx)++] = t[i];
dfs(digits, pos + 1, cidx, cbuf, ridx, rbuf);
}
*cidx -= 1;
return 0;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
char** letterCombinations(char *digits, int *returnSize)
{
int i = 0;
char **rbuf = NULL;
char cbuf[256] = { 0 };
rbuf = calloc(256, sizeof(char *));
for (i = 0; i < 256; i++) {
rbuf[i] = calloc(256, sizeof(char));
}
*returnSize = 0;
if (strlen(digits) == 0) {
return rbuf;
}
i = 0;
dfs(digits, 0, &i, cbuf, returnSize, rbuf);
return rbuf;
}