Steward
分享是一種喜悅、更是一種幸福
程式語言 - LeetCode - C - 1268. Search Suggestions System
參考資訊:
https://www.cnblogs.com/grandyang/p/15679711.html
題目:

解答:
int mysort(const void* a, const void* b)
{
return strcmp(*(char**)a, *(char**)b);
}
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
char*** suggestedProducts(char** products, int productsSize, char* searchWord, int* returnSize, int** returnColumnSizes)
{
int i = 0;
int j = 0;
int len = strlen(searchWord);
char ***r = NULL;
qsort(products, productsSize, sizeof(char**), mysort);
(*returnSize) = 0;
*returnColumnSizes = calloc(len, sizeof(int));
r = calloc(len, sizeof(char**));
for (i = 0; i < len; i++) {
r[i] = calloc(3, sizeof(char*));
for (j = 0; j < 3; j++) {
r[i][j] = calloc(3000, sizeof(char) * 3000);
}
int cnt = 0;
for (j = 0; j < productsSize; j++) {
if (strlen(products[j]) < (i + 1)) {
continue;
}
if (!memcmp(products[j], searchWord, i + 1)) {
strcpy(r[i][cnt], products[j]);
cnt += 1;
if (cnt >= 3) {
break;
}
}
}
(*returnSize) += 1;
(*returnColumnSizes)[i] = cnt;
}
return r;
}