题目大意:给出一组区间,合并他们。

首先是排序,首先看start,start小的在前面。start相同的话,end小的在前面。

排序以后,要合并了。

我自己的笨方法,说实在的问题真的很多。提交了好几次才成功。

[1,2] [1, 3] [2,4]

我的判断标准是 a[i].end > a[i+1].start

很肤浅啊! 直到提示WA。给出了输入是类似这种。[1,10]  [1,3] [8,9]

后面不连续了,但是仍然被前面覆盖。

所以,应该以第i个作为一个区间,不断扩充它的end值。

直到和后面那个完全不重合了,才停止。

PS:自己写一个快速排序,也是问题多多啊!!!

首先是交换两个元素swap,都不会写了!!!想当然的就写错了!!!

然后,结构体直接赋值,居然也疑惑了,怀疑能不能直接赋值。。。

最后,判断cmp也错了。。。

/**
* 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().
*/
bool cmp(struct Interval x, struct Interval y)
{
return x.start < y.start || x.start == y.start && x.end < y.end;
} void swap(struct Interval *x, struct Interval *y)
{
struct Interval tmp = *x;
*x = *y;
*y = tmp;
} int partition(struct Interval* a, int n)
{
int last = n-;
int i,j;
for(i = ,j=; i < n;i++)
{
if(cmp(a[i],a[last]))
{
swap(&a[i],&a[j]);
j++;
}
}
swap(&a[j],&a[last]);
return j;
}
void quick_sort(struct Interval *a ,int n)
{
int j;
if(n < ) return; j = partition(a,n);
quick_sort(a,j);
quick_sort(a+j+,n--j);
} struct Interval* merge(struct Interval* intervals, int intervalsSize, int* returnSize) {
struct Interval *a = intervals;
int n = intervalsSize; if(n < ) return a; struct Interval* ans = (struct Interval* )malloc(n*sizeof(struct Interval));
int len = ; quick_sort(a,n); for(int i = ; i < n; i ++)
{
int max_end = a[i].end;
int min_start = a[i].start; while(i < n- && a[i+].start <= max_end) //a[i].end > a[i+1].start //只要下一个数的起始点还在这个大区间内。
{
i++;
if(a[i].end > max_end) max_end = a[i].end;
}
ans[len].start = min_start;
ans[len].end = max_end;
len ++;
}
*returnSize = len;
return ans;
}

while的代码是,只要下一个值的start比最大的end小,就更新end 的值。

最后把整个的区间放到结果里面。

第二种方式,参考了网上的方法。更合理,更不易出错。

先放入第一个区间,然后对于每一个区间,判断和ans中的最后一个区间是否重叠,有重叠就合并。没有就新增。

struct Interval* merge(struct Interval* intervals, int intervalsSize, int* returnSize) {
struct Interval *a = intervals;
int n = intervalsSize; if(n < ) return a; struct Interval* ans = (struct Interval* )malloc(n*sizeof(struct Interval));
int len = ; quick_sort(a,n); ans[] = a[];
len = ; for(int i = ; i < n; i ++)
{
if(ans[len-].end < a[i].start) //没有交集
{
ans[len].start = a[i].start;
ans[len].end = a[i].end;
len ++;
}
else //有交集,取最大的。
{
ans[len-].end = ans[len-].end > a[i].end ? ans[len-].end : a[i].end;
}
}
*returnSize = len;
return ans;
}

LeetCode 题解 56. Merge Intervals的更多相关文章

  1. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  2. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  3. leetcode个人题解——#56 Merge Intervals

    思路,先按照结构体中start进行排序,然后遍历比较前后项是否有重合. 第一次用到三参数形式的sort(),第三个参数的bool函数要写到类外才通过. /** * Definition for an ...

  4. 【一天一道LeetCode】#56. Merge Intervals

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  5. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. LeetCode OJ 56. Merge Intervals

    题目 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6], ...

  7. [leetcode sort]56. Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  8. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  9. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

随机推荐

  1. 信息安全-加密:AES 加密

    ylbtech-信息安全-加密:AES 加密 高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一 ...

  2. Spring MVC + Mybatis项目搭建

    1.参考<Java Spring MVC项目搭建(一)——Spring MVC框架集成>配置spring mvc需要的jar包及eclipse配置(主要是针对servlet-api.jar ...

  3. scheduler定时器相关

    定时器官网: http://www.quartz-scheduler.org/

  4. SSH配置文件详解

    SSH:是一种安全通道协议,主要用来实现字符界面的远程登录,远程复制等功能. 在RHEL系统中SSH使用的是OpenSSH服务器,由opensh,openssh-server等软件包提供的. sshd ...

  5. 关于jQuery点击事件叠加问题

    先来看个例子: html: <body> <button id="btn">按钮</button> <button id="bt ...

  6. jscs sublime 插件配置 .jscsrc 文件

    { "disallowEmptyBlocks": true, "disallowKeywordsOnNewLine": ["else", & ...

  7. 电商系统架构总结4(webapi 版本控制)

    为了 顺利迭代升级,web api 在维护过程是不断升级的,但用户是不能强迫他们每次都跟随你去升级,这样会让用户不胜其烦.为了保证不同版本的客户端能同时兼容,在web api接口上加入版本控制就很有必 ...

  8. 11 vim文本编辑器

    和sed相比,sed为字处理器(行编辑器),将文本逐行放入到模式空间(也就是内存)中进行处理,并显示在屏幕上.而vim.vi以及nano都是全屏文本编辑器,而vim则是vi的加强版本,相对于vi,vi ...

  9. angularjs的ng-class

    <!--第一种 直接加变量--> <div ng-class="tempClass"></div> <!--第二种 用{{}} 包住的变量 ...

  10. 关于js中的时间——计算时间差等

    获取当前(系统)时间: var NowDate= new Date(); // 获取当前日期时间 // 输出为: Wed May 03 2017 14:52:08 GMT+0800 (中国标准时间) ...