美文网首页
Leetcode 57. Insert Interval

Leetcode 57. Insert Interval

作者: persistent100 | 来源:发表于2017-06-16 16:57 被阅读0次

    题目

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
    You may assume that the intervals were initially sorted according to their start times.
    Example 1:
    Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].
    Example 2:
    Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].
    This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

    分析

    跟上一道题的思路一样,直接把需要插入的区间和之前的区间进行排序,然后使用上道题的程序跑就可以。不明白这道题为啥标的是Hard,而上道题是Medium。

    void sort(struct Interval *a, int left, int right)//快速排序
    {
        if(left >= right)/*如果左边索引大于或者等于右边的索引就代表已经整理完成一个组了*/
        {
            return ;
        }
        int i = left;
        int j = right;
        int key = a[left].start;
        int temp=a[left].end;
    
        while(i < j)                               /*控制在当组内寻找一遍*/
        {
            while(i < j && key <= a[j].start)
            /*而寻找结束的条件就是,1,找到一个小于或者大于key的数(大于或小于取决于你想升
            序还是降序)2,没有符合条件1的,并且i与j的大小没有反转*/ 
            {
                j--;/*向前寻找*/
            }
    
            a[i].start = a[j].start;
            a[i].end=a[j].end;
            /*找到一个这样的数后就把它赋给前面的被拿走的i的值(如果第一次循环且key是
            a[left],那么就是给key)*/
    
            while(i < j && key >= a[i].start)
            /*这是i在当组内向前寻找,同上,不过注意与key的大小关系停止循环和上面相反,
            因为排序思想是把数往两边扔,所以左右两边的数大小与key的关系相反*/
            {
                i++;
            }
    
            a[j].start = a[i].start;
            a[j].end=a[i].end;
        }
    
        a[i].start = key;/*当在当组内找完一遍以后就把中间数key回归*/
        a[i].end=temp;
        sort(a, left, i - 1);/*最后用同样的方式对分出来的左边的小组进行同上的做法*/
        sort(a, i + 1, right);/*用同样的方式对分出来的右边的小组进行同上的做法*/
                           /*当然最后可能会出现很多分左右,直到每一组的i = j 为止*/
    }
    /**
     * Definition for an interval.
     * struct Interval {
     *     int start;
     *     int end;
     * };
     */
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    struct Interval* insert(struct Interval* intervals, int intervalsSize, struct Interval newInterval, int* returnSize) {
        struct Interval *ans=(struct Interval *)malloc(sizeof(struct Interval)*(intervalsSize+1));
        *returnSize=0;
        struct Interval *temp=(struct Interval *)malloc(sizeof(struct Interval)*(intervalsSize+1));
        for(int i=0;i<intervalsSize;i++)
        {
            temp[i].start=intervals[i].start;
            temp[i].end=intervals[i].end;
        }
        temp[intervalsSize].start=newInterval.start;
        temp[intervalsSize].end=newInterval.end;
        sort(temp,0,intervalsSize);
        int p=temp[0].start,q=temp[0].end;
        for(int i=0;i<=intervalsSize;i++)
        {
            printf("%d %d\n",temp[i].start,temp[i].end);
        }
        for(int i=1;i<=intervalsSize;i++)
        {
            if(q>=temp[i].start)
            {
                if(q<temp[i].end)
                    q=temp[i].end;
            }
            else
            {
                ans[*returnSize].start=p;
                ans[*returnSize].end=q;
                *returnSize=*returnSize+1;
                p=temp[i].start;
                q=temp[i].end;
            }
        }
        ans[*returnSize].start=p;
        ans[*returnSize].end=q;
        *returnSize=*returnSize+1;
        return ans;
    }
    

    相关文章

      网友评论

          本文标题:Leetcode 57. Insert Interval

          本文链接:https://www.haomeiwen.com/subject/znjmqxtx.html