题目链接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. 洛谷10月月赛II

    #A: P4924 [1007]魔法少女小Scarlet 这道题考了矩阵旋转 其实很考验推公式的能力和代码能力 这里有个小技巧 可以设(x, y)为原点,然后去推公式,然后实际操作中横坐标加上x,纵坐 ...

  2. 洛谷P1004 方格取数

    网络流大法吼 不想用DP的我选择了用网络流-- 建模方法: 从源点向(1,1)连一条容量为2(走两次),费用为0的边 从(n,n)向汇点连一条容量为2,费用为0的边 每个方格向右边和下边的方格连一条容 ...

  3. MyBatis学习总结(6)——调用存储过程

    一.提出需求 查询得到男性或女性的数量, 如果传入的是0就女性否则是男性 二.准备数据库表和存储过程 create table p_user( id int primary key auto_incr ...

  4. 洛谷 P3047 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 题目描述 Farmer John has noticed that his cows often move between near ...

  5. POJ 2447

    挺水的一题.其实只要理解了RSA算法,就知道要使用大整数分解的方法来直接模拟了. 不过,要注意两个INT64的数相乘来超范围 #include <iostream> #include &l ...

  6. git batch

    git batch 不用每次自己写了:不是特别推荐哦: git add . git commit -m "commit" git push git status

  7. Maven导入ojdbc14.jar和ojdbc6.jar

    Maven导入ojdbc14.jar和ojdbc6.jar 学习了:http://blog.csdn.net/johon_medison/article/details/51689690 在 ‘运行’ ...

  8. c/c++常见试题

  9. ECMAScript 6新特性之Proxy

    ECMAScript 6中新增了一个全局构造函数:Proxy.该构造函数能够接收两个參数:一个目标对象.一个处理对象. 代码演示样例: var target = {}; var handler = { ...

  10. MongoDB数据模型和索引学习总结

    MongoDB数据模型和索引学习总结 1. MongoDB数据模型: MongoDB数据存储结构: MongoDB针对文档(大文件採用GridFS协议)採用BSON(binary json,採用二进制 ...