[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 ...
随机推荐
- MVC缓存01,使用控制器缓存或数据层缓存
对一些浏览频次多.数据量大的数据,使用缓存会比较好,而对一些浏览频次低,或内容因用户不同的,不太适合使用缓存. 在控制器层面,MVC为我们提供了OutputCacheAttribute特性:在数据 ...
- ASP.NET MVC使用Bundle来打包压缩js和css
Bundle它是用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原始方式显示出来,以方便查找问题. 1.BundleConfig配置Bundl ...
- map find 是线程安全的吗
测试环境gcc4.8.2 iterator find ( const key_type& k ); const_iterator find ( const key_type& ...
- djcelery的细节篇
http://blog.csdn.net/samed/article/details/50598371 随时撸一撸,要点记心间.. 1. 下面讲解一下celery.py文件的配置内容,为何要这么配置. ...
- <转> jsp:include 乱码问题解决
jsp include页面出现乱码问题的几种通用解决方法: 1.当jsp include动态文件时(jsp文件)可以在被include的jsp文件头部加上代码: <%@ page languag ...
- 初探数位DP-hdu2089
一开始刷dp就遇到了数位dp,以前程序设计艺术上看过一点,基本没懂,于是趁今天遇到题目,想把它搞会,但就目前状态来看仍然是似懂非懂啊,以后还要反复搞 统计区间[l,r]的满足题意的数的个数,可以转换成 ...
- 11g新特性-如何禁用自动统计信息收集作业
一.11g中auto stats gather job被集成到了auto task中. SQL> select client_name,status from DBA_AUTOTASK_CLIE ...
- C# 对象转换为byte[] ,byte[]还原对象
/// <summary> /// 将一个object对象序列化,返回一个byte[] /// </summary> /// <param name ...
- customTextbox
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 组合数问题hdu5894
http://acm.hdu.edu.cn/showproblem.php?pid=5894 题意如上