void Insertintervals(int* interval, int* returnSize, int** returnColumnSizes, int** ans){ int* tmp = malloc(sizeof(int) * 2); memcpy(tmp, interval, sizeof(int) * 2); (*returnColumnSizes)[*returnSize] = 2; ans[(*returnSize)++] = tmp; } void InsertnewInterval(bool* placed, int* returnSize, int** returnColumnSizes, int** ans, int left, int right){ int* tmp = malloc(sizeof(int) * 2); tmp[0] = left, tmp[1] = right; (*returnColumnSizes)[*returnSize] = 2; ans[(*returnSize)++] = tmp; *placed = true; } int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes) { *returnSize = 0; int left = newInterval[0]; int right = newInterval[1]; bool placed = false; int** ans = malloc(sizeof(int*) * (intervalsSize + 1)); *returnColumnSizes = malloc(sizeof(int*) * (intervalsSize + 1)); for (int i = 0; i < intervalsSize; ++i) { int* interval = intervals[i]; if (interval[0] > right) { if (!placed) // 在插入区间的右侧且无交集 InsertnewInterval(&placed,returnSize,returnColumnSizes,ans,left,right); Insertintervals(interval,returnSize,returnColumnSizes,ans); } else if (interval[1] < left) { // 在插入区间的左侧且无交集 Insertintervals(interval,returnSize,returnColumnSizes,ans); } else { // 与插入区间有交集,计算它们的并集 left = fmin(left, interval[0]); right = fmax(right, interval[1]); } } if (!placed) InsertnewInterval(&placed,returnSize,returnColumnSizes,ans,left,right); return ans; }
int** insert(int** intervals, int intervalsSize, int* intervalsColSize, int* newInterval, int newIntervalSize, int* returnSize, int** returnColumnSizes){ int i,j,pst=0; int** arr = (int**)calloc(intervalsSize+1,sizeof(int*)); int* retCol = (int*)calloc(intervalsSize+1,sizeof(int)); if (intervalsSize==0) { arr[0]=newInterval; *returnSize=1; (*returnColumnSizes)[0]=2; return arr; } for (i=0; i<intervalsSize; i++) { arr[pst] = (int*)calloc(2,sizeof(int)); //newInterval的右边最大值大于比intervals当前一维数组的最大值 if (newInterval[1]>=intervals[i][1]) { if (newInterval[0]>=intervals[i][0] && newInterval[0]<=intervals[i][1]) { arr[pst][0]=intervals[i][0]; arr[pst][1]=newInterval[1]; newInterval[0]=arr[pst][0]; } else if (newInterval[0]<intervals[i][0]) { arr[pst][0]=newInterval[0]; arr[pst][1]=newInterval[1]; } else if (newInterval[0]>intervals[i][1]) { arr[pst][0]=intervals[i][0]; arr[pst][1]=intervals[i][1]; pst++; } } //newInterval的右边最大值在intervals当前一维数组范围内 else if (newInterval[1]>=intervals[i][0] && newInterval[1]<intervals[i][1]) { if (newInterval[0]<intervals[i][0]) { arr[pst][0]=newInterval[0]; arr[pst][1]=intervals[i][1]; } else { arr[pst][0]=intervals[i][0]; arr[pst][1]=intervals[i][1]; } newInterval[0]=arr[pst][0]; newInterval[1]=arr[pst][1]; } //newInterval的右边最大值小于intervals当前一维数组左边最小值 else if(newInterval[1]<intervals[i][0]) { arr[pst][0]=newInterval[0]; arr[pst++][1]=newInterval[1]; for (j=i; j<intervalsSize; j++) { arr[pst] = (int*)calloc(2,sizeof(int)); arr[pst][0]=intervals[j][0]; arr[pst++][1]=intervals[j][1]; } break; } } //特殊情况newInterval和intervals都为1,intervals范围包含newInterval if (pst==0) pst=1; //newInterval的左边最小值大于intervals的最大值 if (newInterval[0]>arr[pst-1][1]) { arr[pst] = (int*)calloc(2,sizeof(int)); arr[pst][0]=newInterval[0]; arr[pst++][1]=newInterval[1]; } // int* retCol = (int*)calloc(pst,sizeof(int)); for (i=0; i<pst; i++) retCol[i]=2; *returnSize=pst; *returnColumnSizes=retCol; return arr; }