题目链接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. CF833B The Bakery (线段树+DP)

    题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值 考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写 ...

  2. man帮助命令

    linux系统提供可比较丰富的帮助手册,man是manual(说明书)的缩写,在日常的linux系统管理中经常会用到. 语法: man(选项)(参数) 选项: -a:在所有的man帮助手册中搜索: - ...

  3. CentOS安装记录

    决定开始复习Linux系统编程,这次我不再折腾Linux下的各种工具,直接使用VS2017进行代码编写与调试. 配置项 值 VM VMware® Workstation 15 Pro OS CentO ...

  4. 《你又怎么了我错了行了吧》第九次团队作业:Beta冲刺与验收准备

    项目 内容 这个作业属于哪个课程 软件工程 这个作业的要求在哪里 实验十三 团队作业9 团队名称 你又怎么了我错了行了吧 作业学习目标 (1)掌握软件黑盒测试技术: (2)学会编制软件项目总结PPT. ...

  5. [luogu] P3333 [ZJOI2013]丽洁体(贪心)

    P3333 [ZJOI2013]丽洁体 题目描述 平时的练习和考试中,我们经常会碰上这样的题:命题人给出一个例句,要我们类比着写句子.这种往往被称为仿写的题,不单单出现在小学生的考试中,也有时会出现在 ...

  6. C#-GC基础(待补充)

    Finalize方法与Dispose方法区别 1. Finalize只释放非托管资源: 2. Dispose释放托管和非托管资源: // D 是神的天敌3. 重复调用Finalize和Dispose是 ...

  7. AssetBundle打包优化解决方式

    第一阶段:AssetBundle出一套解决方式 1.解决如今同一个资源打2个bundle的冗余问题 2.測试验证节省资源的比率是多少 问题拆分 一.bundle反复 问  题  :同样资源拆分问题? ...

  8. java 日期和字符串互转,依据当天整天时间 得到当天最后一秒的日期时间

    java 日期和字符串互转.依据当天整天时间   得到当天最后一秒的日期时间 package com.hi; import java.text.DateFormat; import java.text ...

  9. 练练脑,继续过Hard题目

    http://www.cnblogs.com/charlesblc/p/6384132.html 继续过Hard模式的题目吧.   # Title Editorial Acceptance Diffi ...

  10. 随心所欲生成git仓库随意一段commit的专用patch应用小实践

     随心所欲生成git仓库随意一段commit的专用patch应用小实践 我们在开发中.时不时的可能要去做一个patch给你的下线,或者你的合作者.在git管理中,我们知道有git format-pat ...