【leetcode】210. 课程表 II
int* findOrder(int numCourses, int** prerequisites, int prerequisitesSize, int* prerequisitesColSize, int* returnSize){ int** edges = (int**)calloc(numCourses, sizeof(int*)); int* edgesColSize = (int*)calloc(numCourses, sizeof(int)); int* cnt = (int*)calloc(numCourses, sizeof(int)); int* arr = (int*)calloc(numCourses, sizeof(int)); *returnSize = 0; int i, a, b, left=0; for (i = 0; i < numCourses; i++) edges[i] = (int*)malloc(0); for (i = 0; i < prerequisitesSize; i++){ a = prerequisites[i][1]; b = prerequisites[i][0]; edges[a] = (int*)realloc(edges[a], sizeof(int)* ++edgesColSize[a]); edges[a][edgesColSize[a] - 1] = b; cnt[b]++; } for (i = 0; i < numCourses; i++){ if (cnt[i] == 0) arr[(*returnSize)++] = i; } while (left < *returnSize){ int u = arr[left++]; for (i = 0; i < edgesColSize[u]; i++){ cnt[edges[u][i]]--; if (cnt[edges[u][i]]==0) arr[(*returnSize)++] = edges[u][i]; } } if (*returnSize != numCourses) *returnSize = 0; return arr; }