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], ...
随机推荐
- (四)mysql -- 常用函数
今天get一个,先记录一下 以后慢慢补充~ 将varchar转换成int 例如:select * from tb_1 order by cast(sport_sum as unsigned integ ...
- 使用Runnable接口创建线程池
步骤: 创建线程池对象创建 Runnable 接口子类对象提交 Runnable 接口子类对象关闭线程池实例: class TaskRunnable implements Runnable{ @Ove ...
- 错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误
WIN7 64位旗舰版安装OFFICE2003 提示:“错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误” 在64位系统上,32位软件的注册表的信息不是直接在 ...
- java实验报告&第三周学习总结
实验报告: 实验1:求水仙花数在C语言中求水仙花数是一个复杂且长的代码,在java中可以通过简单的代码来实现操作,找出水仙花数: 图例如下: 试验2:求13+23-33+43-53.........9 ...
- python使用内置方法和修饰器方法获取类名、函数名
1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 getattr(a,'__name__') 2. 内部 ...
- 电商企业如何做好EDM营销随感
对于中小型电商企业来说,运用EDM营销是一种非常不错的营销方式,正如我在电商EDM数据营销中的关键介绍一样.下面博主给大家介绍一下电商企业如何做好EDM营销. 一.在EDM邮件内容中跟客户建立信任的关 ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_7 Mybatis中参数的深入-使用实体类的包装对象作为查询条件
pojo对象就是实体类 综合查询-实体类包装起来做查询 新建实体类QueryVo 提供一个User对象属性,并生成getter和setter 测试 修改dao接口中的返回类型为List<User ...
- NFS服务和DHCP服务讲解
1.NFS服务端概述 NFS,是Network File System的简写,即网络文件系统.网络文件系统是FreeBSD支持的文件系统中的一种,也被称为NFS: NFS允许一个系统在网络上与他人共享 ...
- ES6标准入门 第四章:字符串的扩展
1.字符串的Unicode 表示法 JavaScript 允许采用 \uxxxx 表示一个字符,其中 xxxx 表示字符的码点. "\u0061" // "a" ...
- Java多线程学习——任务定时调度
Timer 本身就是一个线程,最主要的方法就是schedule(). schedule()的参数介绍: schedule(TimerTask task, long delay) //延迟delay毫秒 ...