bool DFS(int num, int **prerequisites, int prerequisitesRowSize, int *result, int *n, char *visit_state)
{
if(visit_state[num] == 1) return false;
if(visit_state[num] == 2) return true;
visit_state[num] = 1;
for(int i=0; i<prerequisitesRowSize; i++) {
int *pair = prerequisites[i];
if(pair[0] == num) {
if(DFS(pair[1], prerequisites, prerequisitesRowSize, result, n, visit_state) == false) {
return false;
}
}
}
result[*n] = num;
(*n);
visit_state[num] = 2;
return true;
}
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* findOrder(int numCourses, int** prerequisites, int prerequisitesRowSize, int prerequisitesColSize, int* returnSize) {
int n = numCourses;
int *result = (int*)malloc(sizeof(int)*n);
char *visit_state = (char*)malloc(sizeof(char)*n);
//memset(visit_state, 0, sizeof(char)*n);
for(int i=0; i<n; i++)
visit_state[i] = 0;
n;
for(int i=0; i<numCourses; i++) {
if(DFS(i, prerequisites, prerequisitesRowSize, result, &n, visit_state) == false) {
free(visit_state);
*returnSize = 0;
return result;
}
}
free(visit_state);
*returnSize = numCourses;
return result;
}
Why does my c code get run time error at input 1,[ ] ?


@hanping Please try again, I have just fixed an issue in the driver code. It's a silly mistake, sorry about that.