[Leetcode] Merge Intevals
Question:
Given a collection of intervals, merge all overlapping intervals.
For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
---------------------------------------
Solution:
public class Solution {
class IntervalAsc implements Comparator<Interval>
{
public int compare (Interval o1, Interval o2)
{
if (o1.start != o2.start)
return o1.start < o2.start ? -1 : 1;
else if (o1.end != o2.end)
return o1.end < o2.end ? -1 : 1;
else
return 0;
}}
public ArrayList<Interval> merge (ArrayList<Interval> intervals)
{
Collections.sort(intervals, new IntervalAsc());
ArrayList<Interval> ret = new ArrayList<Interval>();
int n = intervals.size();
if(n==0) return ret;
Interval last=intervals.get(0);
for(int i=1;i<n;i++){
if(intervals.get(i).start>last.end){
ret.add(new Interval(last.start,last.end));
last=intervals.get(i);
}else{
last.end=Math.max(intervals.get(i).end, last.end);
}
}
ret.add(last);
return ret;
}
}
需要注意的以下几点:
这里需要new一个Interval,再放进ArrayList result里。刚开始我就是直接把last放进了result里,忘记了last只是一个类似于指针一样的东西。- 第一次使用到了内部类。更多的内部类的知识看下一篇博客。
- Collections.sort的用法。(compare函数的重写)
[Leetcode] Merge Intevals的更多相关文章
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] Merge k Sorted Lists 合并k个有序链表
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 这 ...
- [LeetCode] Merge Two Sorted Lists 混合插入有序链表
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- [Leetcode] Merge Sorted Array (C++)
我在Github上新建了一个解答Leetcode问题的Project, 大家可以参考, 目前是Java 为主,里面有leetcode上的题目,解答,还有一些基本的单元测试,方便大家起步. 题目: Gi ...
- LeetCode Merge k Sorted Lists 解决报告
https://oj.leetcode.com/problems/merge-k-sorted-lists/ 归并K已经整理阵列,和分析算法的复杂. 解决报告:无论是不考虑优化,最简单的实现是要重新走 ...
- [LeetCode] Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- [leetcode]Merge Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/merge-sorted-array/ 题意:Given two sorted integer arrays A and B ...
随机推荐
- Linux 配置NFS,文件共享
配置: 1.设定共享主机服务器 ---(注意防火墙) 编辑ipA端的/etc/exports 文件 [root@dbrac2 ~]# cat /etc/exports /media 192 ...
- 2016"百度之星" - 初赛(Astar Round2A)Gym Class(拓扑排序)
Gym Class Accepts: 849 Submissions: 4247 Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65 ...
- 信号量互斥,王明学learn
信号量互斥 信号量(又名:信号灯)与其他进程间通信方式不大相同,主要用途是保护临界资源(进程互斥).进程可以根据它判定是否能够访问某些共享资源.除了用于访问控制外,还可用于进程同步. 一.信号量分类 ...
- Hibernate入门案例 增删改
一.Hibernate入门案例剖析: ①创建实体类Student 并重写toString方法 public class Student { private Integer sid; private I ...
- SQLServer 维护脚本分享(09)相关文件读取
/********************[读取跟踪文件(trc)]********************/ --查看事件类型描述 SELECT tc.name,te.trace_event_id, ...
- SQL作业的操作全
--定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...
- 【POI】使用POI处理xlsx的cell中的超链接 和 插入图片 和 设置打印区域
使用POI对xlsx中插入超链接和 插入图片 package com.it.poiTest; import java.awt.image.BufferedImage; import java.io.B ...
- spring配置的相关文章
1.maven项目中自动下载jar包的pom.xml配置 http://blog.sina.com.cn/s/blog_643634b80101hd3i.html 2.参考配置1:http://www ...
- css新增选择器
- clear both
原文地址:http://www.codefans.net/articles/653.shtml 因CSS很多布局是需要浮动的,当属性设置float(浮动)时,其所在的物理位置已经脱离文档流了,为了使f ...