【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接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进行排序的更多相关文章
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- 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 ...
- [Leetcode Week2]Merge Intervals
Merge Intervals题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-intervals/description/ Descript ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- Qt编程—去掉标题栏和设置窗口透明用法
学习Qt编程,有时候我们很想做出好看又比较炫的画面,这时就常用到qt上的一些技巧. 这里我以一个小例子来展示qt的这些技巧,此qt编程写的,如图:(去掉标题栏和设置窗口透明后) 代码实现部分: .h文 ...
- python_字符串常用操作
name = "monicao"name.capitalize() #首字母大写print(name.capitalize()) print(name.count("o& ...
- oracle 的交并差函数,intersect;union;minus。
创建表并添加数据: --创建TABLE_A create table TABLE_A ( A ), B ) ); --给TABLE_A添加数据 insert into TABLE_A values(' ...
- Docker学习总结(13)——从零开始搭建Jenkins+Docker自动化集成环境
本文只简单标记下大概的步骤,具体搭建各个部分的细节,还请自行搜索.第一.二部分只是对Jenkins和Docker的简单介绍,熟悉的同学请直接跳到第三部分. 一.关于Jenkins Jenkins简介 ...
- SELECT使用子查询
SELECT使用子查询 SELECT使用子查询,该子查询会执行多次, 次数是由记录数量决定.效率比较低,不推荐使用. //查询部门编号,工资大于等于2000的人数, //工资小于2000的人 ...
- Java String.replaceAll()方法
声明 以下是java.lang.String.replaceAll()方法的声明 public String replaceAll(String regex, String replacement) ...
- MySQL创建表时加入的约束以及外键约束的的意义
1,创建表时加入的约束 a) 非空约束,not null b) 唯一约束,unique c) 主键约束,primary key d) 外键约束,foreign key 1,非空约束,针对某个字段设置其 ...
- 王立平-bmp.compress()
bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos); //30 是压缩率,表示压缩70%; 假设不压缩是100,表示压缩率为0
- 敏捷自己主动化单元測试 (从前台 JavaScript 至后台 Java)
此份材料的内容适用于前台 JavaScript 与后台 Java 的单元測试◦ 希望, 能协助开发者可在最短的时间内, 开展单元測试的工作◦ 附件: 敏捷自己主动化单元測试 例子代码: QUnit 例 ...
- L2CAP数据发送和接收
ACL 链路在 Bluetooth 中非常重要,一些重要的应用如 A2DP, 基于 RFCOMM 的应用.BNEP等都要建立 ACL 链路,发送/接收ACL 包.跟大家一起来分析 ACL 包发送/接收 ...