题目链接Merge Intervals

/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
* 题目:LeetCode 第56题 Merge Intervals 区间合并给定一个区间的集合,将相邻区间之间重叠的部分合并
* 思路:现将这个区间的集合依照从大到小排好序,之后遍历假设当前的集合的尾部大于下一个集合的头部。则有重合就合并。并继续推断合并之后区间与下一个区间的头的大小关系,之后放到答案的区间中,假设尾部大于小于下一个区间的头则没有重合,则直接将当前区间放入答案集合中
*
*/ public class Solution {
public List<Interval> merge(List<Interval> intervals) {
if(intervals.size() == 0 || intervals.size() == 1)
return intervals;
List<Interval> answer = new ArrayList<Interval>(); //注意:List是虚拟的类不能够直接初始化,要用它的子类为其初始化,即。假设是new List<Interval>则会出错
Collections.sort(intervals,new intervalComparator()); //先将输入的区间集合排好序 Interval pre = intervals.get(0);
for(int i = 1;i < intervals.size();i++){
Interval curr = intervals.get(i);
if(pre.end < curr.start){ //区间不用合并
answer.add(pre);
pre = curr;
}
else{
Interval merge = new Interval(pre.start,pre.end>curr.end? pre.end:curr.end);
pre = merge;
}
}
answer.add(pre);
return answer;
}
}
class intervalComparator implements Comparator<Interval>{
public int compare(Interval a,Interval b){
return a.start - b.start;
}
}

【LeetCode】Merge Intervals 题解 利用Comparator进行排序的更多相关文章

  1. [LeetCode] Merge Intervals 排序sort

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

  2. LeetCode: Merge Intervals 解题报告

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

  3. [LeetCode] Merge Intervals 合并区间

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

  4. [leetcode]Merge Intervals @ Python

    原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...

  5. Leetcode Merge Intervals

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

  6. LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

    感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...

  7. 56[LeetCode] .Merge Intervals

    Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...

  8. [Leetcode Week2]Merge Intervals

    Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...

  9. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. vue 阿里云上传组件

    vue 阿里云上传组件 Vue.js上传图片到阿里云OSS存储 测试项目git地址 本测试项目启动方法 示例链接 组件配置项 实践解释 本文主要介绍如何 在vue项目中使用web 直传方式上传阿里云o ...

  2. nignx 502错误不能使用/的路径方式 即pathinfo

    在server中加入 include enable-php-pathinfo.conf; 引入nginx.conf下的这个文件即可. 如果是tp框架,主要隐藏index.php的入口文件,再加入下面这 ...

  3. NYIST 677 碟战

    碟战时间限制:2000 ms | 内存限制:65535 KB难度:4 描述知己知彼,百战不殆!在战争中如果被敌人掌握了自己的机密,失败是必然的.K国在一场战争中屡屡失败,就想到自己的某些城市可能会有敌 ...

  4. WinServer-IIS-woff字体不显示问题

    ASP.NET mvc发布到IIS之后,访问网站的时候,发现woff字体没有加载 百度发现很多博客上的教程是这样的,在IIS管理器中的MIME选项中添加类型 但是重新使用IIS发布后,新添加的字体就会 ...

  5. C#-WebService基础01

    来自为知笔记(Wiz)

  6. 怎样注冊 diskgroup 到集群

    之前使用 renamedg  对 ora.CRS.dg 进行重命名ora.DUPCRS.dg 可是 renamedg 有个缺点就是无法将改动的信息同步到整个集群层面,并且改动前的dg 会依旧保留在集群 ...

  7. ZOJ 3329

    方程很明显有 d[i]=sum(pk*d[i+k])+p0*d[0]; 其中pi可以在开始时枚举求出. 设d[i]=A[i]*d[0]+B[i], 代入上式 d[i]=(sum(pk*A[i+k])+ ...

  8. JDK1.7中的ThreadPoolExecutor源代码剖析

    JDK1. 7中的ThreadPoolExecutor 线程池,顾名思义一个线程的池子,池子里存放了非常多能够复用的线程,假设不用线程池相似的容器,每当我们须要创建新的线程时都须要去new Threa ...

  9. Android native CursorWindow数据保存原理

    我们通过Uri查询数据库所得到的数据集,保存在native层的CursorWindow中.CursorWindow的实质是共享内存的抽象,以实现跨进程数据共享.共享内存所採用的实现方式是文件映射. 在 ...

  10. [React] Use the new React Context API

    The React documentation has been warning us for a long time now that context shouldn't be used and t ...