leetcode-1203-NOT

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* sortItems(int n, int m, int* group, int groupSize, int** beforeItems,
 int beforeItemsSize, int* beforeItemsColSize, int* returnSize){

    int ifdone[groupSize];// 记录是否做完
    int* answer = (int *)malloc(sizeof(int) * groupSize);
    int next = 0;
    for(int i = 0;i < groupSize; i++){
    	ifdone[i] = 0;
    	answer[i] = -1;
    }
    // 先循环一遍把0个前置项目的项目给做了
    for(int i = 0; i < n; i++){
    	if (beforeItemsColSize[i] == 0){
    		ifdone[i] = 1;
    		answer[next] = i;
            next +=1;
    	}
    }
    // 一轮一轮的循环,要是哪轮没做新项目,就停止循环开始check
    int flag = 1; // 循环用的flag
    while(flag){
    	flag = 0;
    	for (int i = 0; i < n; i++){
    		if (ifdone[i] == 1)	// 已经做了就跳过
    			continue;
    		int nengzuo = 1;
    		for (int j = 0; j <beforeItemsColSize[i]; j++){
    			if ( ifdone[beforeItems[i][j]] == 0)
    				nengzuo = 0;
    		}
    		if(nengzuo){
    			ifdone[i] = 1;
    			answer[next] = i;
                next +=1;
    			flag = 1;
    		}
    	}
    }
    //输出检验模块
    //for(int i = 0;i < groupSize; i++){
    //	printf("%d",answer[i]);
    //}
    
    //跳出循环了,检查是否能输出
    if(next == n){
        returnSize[0] = n;
    	return answer;
    }else{
        //printf("not");
        returnSize[0] = 0;
        answer[0] = '\n';
    	return answer;
    }
}

没通过,感觉有多种解决方案,但是跟答案不一致就不算通过,不知道是谁的问题。

posted @ 2021-01-12 15:49  RougeBW  阅读(61)  评论(0编辑  收藏  举报