【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 ...
随机推荐
- vue定义对象变量并合并成新的对象
背景: 一般情况下,向后台发送数据请求会存在公共的变量,为了避免每一个相同部分的变量都重新定义,则想出以下解决方案: 例如一下:function,version,Authorization是公共请求部 ...
- 【codeforces 229C】Triangles
[题目链接]:http://codeforces.com/problemset/problem/229/C [题意] 给你一张完全图; 然后1个人从中选择m条边; 然后另外一个人从中选择剩余的n*(n ...
- MyBatis学习总结(5)——实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- DML语句(添加、更新和删除记录)
a.添加记录(一次插入一行记录) insert into 表名(字段名,字段名...) values (字段值,字段值...) insert into person ...
- HDU 3723
把向上看成+1,向下看成-1.可以知道符合卡特兰数的一般解释了.记作Can(i) 中间平过的即是0.亦即是C(n,2*i),i表示向上的数. 于是总的就是sum(C(n,2*i)*Can(i)),i从 ...
- [React] Unit test a React Render Prop component
In this lesson, I use Enzyme and Jest to unit test a Counter Render Prop component. Writing integrat ...
- [Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular
By default the new Angular Http client (introduced in v4.3.1) uses JSON as the data format for commu ...
- what's new in vc2015
1. 变量和函数的注解提示非常实用.象C#了. 2.CStdioFile升级了,不再须要象 vc2013中,用CStdioFileEx来修复错误了. 3. 发现再写.
- codeblocks开源的c、c++编译器,小巧方便
1.下载带gun的版本 2.设置编译的位置 3.创建项目 4.执行项目 有意思的开源的c编译器 ~~~
- linux下挂载ISCSI存储设备
安装 首先要在存储设备上做好RAID,设置好iSCSI 目标方(target). 这里主要说明iSCSI initiator的安装. 不同的操作系统对应各自的iSCSI initiator,以Redh ...