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

首先是排序,首先看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. es6 class函数的用法,及兼容程度

    //es6中 class的新特性:面向对象的方式 class name{ fram(){ var div=document.getElementById("div"); div.s ...

  2. 聊天,发朋友圈可以不打字,但是表情怎么能少呢?那么如何用win10自带的微软拼音输入法打出表情呢?

    Step1:将光标定位到你要输入的地方 Step2:将输入法切换为中文,win10是[Win+空格] Step3:打出想要的表情类型,例如开心,(切记输完后不要选择文字,就保持拼音状态),保持下图的样 ...

  3. [ERR] Node is not empty. Either the node already knows other nodes (check with C

    [root@node00 src]# ./redis-trib.rb add-node --slave --master-id4f6424e47a2275d2b7696bfbf8588e8c4c3a5 ...

  4. sed初学者实用说明

     转自:http://www.codeweblog.com/sed%E5%88%9D%E5%AD%A6%E8%80%85%E5%AE%9E%E7%94%A8%E8%AF%B4%E6%98%8E/ ...

  5. 使用LVS实现负载均衡原理及安装配置详解(转)

    负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均衡设备F5.Netscale.这里主要是学 ...

  6. Solr的Filed中indexed与stored属性

    摘要: Solr的Filed中有很多参数,其中的index和store参数让人有点犯晕. 消失的Field <field name="description_s" type= ...

  7. SDOI2018IIIDX

    /* 题目转换为 n个节点的一片森林,n个权值,要给每个节点分配一个权值,保证子节点的权值不小于父节点的权值,并且1~n的权值的字典序最大. 考场上的贪心很显然 建立出 树来 将所有数值从大到小排序 ...

  8. php添加多组数据到数据库

    //添加sql的数据 $sqldatas=getParam('sqldatas');//这里的sqldatas是从前台传过来的json字符串 //将json字符串转为json对象 $sqldata=j ...

  9. vue build打包后css里的图片路径404不正确的问题

    vue build打包后css里的图片路径404 在vue-cli项目中build/utils.js中找到如下代码块,添加 publicPath:’../../’ if (options.extrac ...

  10. inno setup 软件打包

    2017-05-26 软件开发完毕以后,要进行打包发布,有几款打包用的软件很常见:innosetup,nullsoft,installshield.本人今天终于把自己独立开发的呈现软件打了包.第一个版 ...