56. Merge Intervals (JAVA)
Given a collection of intervals, merge all overlapping intervals.
Example 1:
Input: [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].
Example 2:
Input: [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.
NOTE: input types have been changed on April 15, 2019. Please reset to default code definition to get new method signature.
涉及:
- 数组的截取与拷贝
- 重写Array.sort的compare函数(注意:jdk1.7以后,必须要定义相等的情况返回0,否则会报Runtime error)
class Solution {
public int[][] merge(int[][] intervals) {
if(intervals.length == 0) return intervals;
Arrays.sort(intervals,0, intervals.length, new ArrayComparator());
int curIndex = 0;
for(int i = 1; i < intervals.length; i++){
if(intervals[curIndex][1] >= intervals[i][1]){ //only reserve intervals[i-1]
continue;
}
else if(intervals[curIndex][1] >= intervals[i][0]){//integrate
intervals[curIndex][1] = intervals[i][1];
}
else{ //no interval
curIndex++;
intervals[curIndex][0] = intervals[i][0];
intervals[curIndex][1] = intervals[i][1];
}
}
curIndex++;
int[][] ret = new int[curIndex][2];
System.arraycopy(intervals, 0, ret, 0, curIndex);
return ret;
}
}
class ArrayComparator implements Comparator{
public int compare(Object o1, Object o2){
int[] a = (int[]) o1;
int[] b = (int[]) o2;
if(a[0] > b[0]) return 1;//ascending order
else if(a[0] < b[0]) return -1;
else return 0;
}
}
56. Merge Intervals (JAVA)的更多相关文章
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 56. Merge Intervals
题目: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6], ...
随机推荐
- 小程序的image图片显示
最近接触到了一点小程序的东西,跟我们平常的HTML还真不太一样,这里我先大概讲一下图片的显示问题, 小程序的图片用的是<image><image/>标签,他默认的大小是宽300 ...
- [CSP-S模拟测试]:bird(线段树优化DP)
题目传送门(内部题89) 输入格式 第一行两个数$n$和$k$,分别表示小鸟的只数和$R$装弹时间.接下来$n$行,每行两个数$l,r$表示$n$只小鸟初始时的头和尾的$x$坐标. 输出格式 输出一个 ...
- NOIP2009靶形数独(暴搜)
题目传送门 题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z博士请教,Z博士拿出了他最近发明 ...
- Comparable接口与Comparator接口的比较————Comparator接口详解
Comparator接口位于:java.util包中. Comparator接口:1. 强行对某个对象的Collection进行整体排序.值得注意的是:Comparator接口可以作为参数传到一些so ...
- 大数据笔记(二十九)——RDD简介、特性及常用算子
1.什么是RDD? 最核心 (*)弹性分布式数据集,Resilent distributed DataSet (*)Spark中数据的基本抽象 (*)结合源码,查看RDD的概念 RDD属性 * Int ...
- 《Effective Java》读书笔记 - 3.对于所有对象都通用的方法
Chapter 3 Methods Common to All Objects Item 8: Obey the general contract when overriding equals 以下几 ...
- Ping链路测试
https://help.aliyun.com/knowledge_detail/40573.html?spm=5176.2020520165.121.d519.4b4f7029sHzfmi#TRAC ...
- js fuction函数内return一个内部函数详解
今天在网上,看到一篇关于js函数难点的文章,js函数的一些难点.在那上面提了一下,关于js函数返回另一个函数的问题,并附上了一道面试题: var add = function(x){ var sum ...
- docker英语
demotevt. 使降级:使降职 promotevt. 促进:提升:推销:发扬 swarmn. 一大群:蜂群:人群:一大群小型天体同时在空中出现 worker 工人manager 经理swarm 人 ...
- 图解http协议学习笔记
一 ,基本概念 1互联网相关的各协议族为tcp/ip协议(网际协议),tcp/ip ftp,DNS(通过域名解析ip地址),http(超文本传输协议) 还有很多协议 ,只是列举比较熟悉的 2tcp/ ...