/**
* 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().
*/
int** intervalIntersection(int** firstList, int firstListSize, int* firstListColSize, int** secondList, int secondListSize, int* secondListColSize, int* returnSize, int** returnColumnSizes){
int ** returnSection = (int **)malloc(sizeof(int *) *2000);
*returnSize = 0;
*returnColumnSizes = (int *)malloc(sizeof(int)*2000);
if(firstListSize==0){
return firstList;
}
if(secondListSize==0){
return secondList;
}
for(int a=0;a<firstListSize;a++){
for(int b=0;b<secondListSize;b++){
if(firstList[a][1]==secondList[b][0]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = firstList[a][1];
returnSection[*returnSize][1] = secondList[b][0];
*returnSize = *returnSize + 1;
}
else if(firstList[a][1]<secondList[b][0]){
break;
}
else if(firstList[a][0]==secondList[b][1]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = firstList[a][0];
returnSection[*returnSize][1] = secondList[b][1];
*returnSize = *returnSize + 1;
}
else if(firstList[a][0]<secondList[b][0]&&firstList[a][1]<secondList[b][1]){
if(firstList[a][1]>secondList[b][0]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = secondList[b][0];
returnSection[*returnSize][1] = firstList[a][1];
*returnSize = *returnSize + 1;
}
}
else if(firstList[a][0]<=secondList[b][0]&&secondList[b][1]<=firstList[a][1]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = secondList[b][0];
returnSection[*returnSize][1] = secondList[b][1];
*returnSize = *returnSize + 1;
}
else if(firstList[a][0]>=secondList[b][0]&&secondList[b][1]>=firstList[a][1]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = firstList[a][0];
returnSection[*returnSize][1] = firstList[a][1];
*returnSize = *returnSize + 1;
}
else if(firstList[a][0]==secondList[b][0]&&secondList[b][1]==firstList[a][1]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = firstList[a][0];
returnSection[*returnSize][1] = firstList[a][1];
*returnSize = *returnSize + 1;
}
else if(firstList[a][0]>=secondList[b][0]&&secondList[b][1]<=firstList[a][1]){
if(firstList[a][0]<=secondList[b][1]){
returnSection[*returnSize] = (int *)malloc(sizeof(int)* 2 );
(*returnColumnSizes)[*returnSize]= 2;
returnSection[*returnSize][0] = firstList[a][0];
returnSection[*returnSize][1] = secondList[b][1];
*returnSize = *returnSize + 1;
}
}
}
}
return returnSection;
}
力扣------区间列表的交集
最新推荐文章于 2024-07-19 22:53:00 发布