[Problem solution] merge interval

Title description
Given a set of intervals, please merge all overlapping intervals.

示例 1:
输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:
输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

Here I mainly talk about the qsort() function to be used.
Function prototype:
void qsort(void *base, size_t nitems, size_t size, int (*compar)(const void , const void ))
parameters

  • base-- Pointer to the first element of the array to be sorted.
  • nitems-the number of elements in the array pointed to by base.
  • size--The size of each element in the array, in bytes.
  • compar-- A function used to compare two elements, that is, a function pointer (callback function)

And the compar() function inside.
Function prototype:
int compar(const void *p1, const void *p2);
We wrote a comp() function to perform this step, and comp will return an integer to qsort(). Then qsort will operate elements according to the following rules.

  • If the return value of compar is less than 0 (< 0), then the element pointed to by p1 will be sorted to the left of the element pointed to by p2;
  • If the return value of compar is equal to 0 (= 0), then the order of the element pointed to by p1 and the element pointed to by p2 is uncertain;
  • If the return value of compar is greater than 0 (> 0), then the element pointed to by p1 will be sorted to the right of the element pointed to by p2.

Lizou compiler running

/**
 * 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().
 */
//下面定义的四个常量,因为后面没有数值,所以编译器会自动省略,这里只是为了代码规范。
// 定义输入输出参数
#define IN
#define OUT
// 定义框架
#define __________FRAME_START__________
#define __________FRAME_END__________

//为后面的qsort() 函数调用时使用
static int comp(const void* a, const void* b)
{
    
    
    return *(int*)a - *(int*)b;
}

//intervals 数组指针,intervalsSize 数组行数,intervalsColSize 数组列数指针
//returnSize 返回数组行数,returnColumnSizes 返回数组列数的指针
int** merge(IN int** intervals, IN int intervalsSize, IN int* intervalsColSize, OUT int* returnSize, OUT int** returnColumnSizes){
    
    
    //划分空间,等一下用来存储代表开头和结束两个不同的数据
    int* pStart = (int*)malloc(intervalsSize * sizeof(int));
    int* pEnd = (int*)malloc(intervalsSize * sizeof(int));

    __________FRAME_START__________

    int row = 0;    // 定义行列
    int col = 0;    //定义列数
    *returnSize = 0;    // 初始化returnSize

    //在堆空间中分配二维数组,用malloc
    //定义返回数组的二级指针,元素个数为行数
    int** pRes = (int**)malloc(intervalsSize * sizeof(int*));

    //创建返回数组,并将数据分类
    for (row = 0; row <= intervalsSize - 1; row++)  //以行数来作为循环结束条件
    {
    
    
        pRes[row] = (int*)malloc(*intervalsColSize * sizeof(int)); // 为每个结点分配一个数组空间,元素个数为列数

        //数据分类,开头数字存放到开始(pStart)数组中,结尾数字同理
        pStart[row] = intervals[row][0];    //将区间的开头数字存放到pStart 数组中
        pEnd[row] = intervals[row][1];      //将区间的结束数字存放到pEnd 数组中
    }

    //初始化指针,元素个数为行数,每个元素用来存放该行的列数
    *returnColumnSizes = (int*)malloc(intervalsSize * sizeof(int));

    __________FRAME_END__________

    //将pStart 和 pEnd 两个数据按照从小到大的顺序进行排序
    qsort(pStart, intervalsSize, sizeof(int), comp);
    qsort(pEnd, intervalsSize, sizeof(int), comp);


    for (row = 0; row <= intervalsSize - 1; row++)
    {
    
    
        __________FRAME_START__________

        // 为返回数组的开头赋值,此处用到了returnSize指针变量,其内容是行数下标,因此注意要加*
        pRes[*returnSize][0] = pStart[row];

        //判断是否有开头小于上一个结尾的,如果有则合并到上一个数组中
        for ( ; row <= intervalsSize - 2; row++)
        {
    
    
            if (pStart[row + 1] > pEnd[row])
            {
    
    
                break;
            }
        }

        //为返回数组的结尾赋值
        pRes[*returnSize][1] = pEnd[row];

        //为returnColumnSizes 赋值列数值
        //注意[]的优先级比*高,所以要加括号。因为我们是为*returnColumnSizes 分配的空间,没对returnColumnSizes分配过空间
        (*returnColumnSizes)[*returnSize] = 2;

        //进入下一列
        (*returnSize)++;

        __________FRAME_END__________
    }
    //返回合并好的数组
    return pRes;
}

Update progress this month 5/15

Creation is not easy, your likes are my biggest motivation! ! !
See you next time end~

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43776724/article/details/106904928