34.Merge Intervals(合并序列)
Level:
Medium
题目描述:
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.
思路分析:
定义一个数据类型interval
public class Interval{
int start; //序列对的首元素
int end;//序列对的尾元素
public Interval(int s,int e){
start=s;
end=e;
}
}
将题目给出的所有序列对的首元素都保存在一个数组starts,将所有尾元素保存在另外一个数组ends,然后将两个数组排序,设置两个指针,i 和 j。从头开始遍历数组,如果starts[i+1]>ends[i],那么产生一个以ends[i]作为尾部的Interval,头部为starts[j],j的初始值是0,随后每产生一个Interval,j 就更新为(i+1);当i==n-1时,也会产生一个最后一个Interval。
代码:
/**
* 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; }
* }
*/
public class Solution{
public List<Interval>merge(List<Interval>intervals){
List<Interval>finalList=new AarryList<>();
int []starts=new int[intervals.size()];
int []ends=new int[intervals.size()];
for(int i=0;i<intervals.size();i++){
starts[i]=intervals.get(i).start;
ends[i]=intervals.get(i).end;
}
Arrays.sort(starts);
Arrays.sort(end);
for(int i=0,j=0;i<starts.length;i++){
if(i==n-1||starts[i+1]>ends[i]){
Interval interval=new Interval(starts[j],ends[i]);
finalList.add(interval);
j=i+1;
}
}
return finalList;
}
}
34.Merge Intervals(合并序列)的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- merge intervals(合并间隔)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode每天一题】Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 如何用CSS定义一个动画?
<style type="text/css"> div{ width:100px;height: 100px; animation: carton 5s; backgr ...
- javaweb各种框架组合案例(四):maven+spring+springMVC+spring data jpa(hibernate)【失败案例】
一.失败案例 1. 控制台报错信息 严重: Exception sending context initialized event to listener instance of class org. ...
- 1、pip不是内部运行程序 解决方法
一.方式一 1.切换到pip所在路径: shit+ 右键. 再此处打开运行窗口 2.执行 pip install pytest 脚本即可. 二.方式二,添加环境变量 1.将pip所在的文件路径 添加到 ...
- pandas模块之读取文件
首先我们来看一个文件 1 男 北京 刘一 我笑 #跳过此行,序号1 2 女 上海 刘珊 你笑 3 男 杭州 刘五 他笑 #跳过此行,序号四 4 女 重庆 刘六 不笑了 下面来分析内容,并使用参数 1 ...
- 箭头函数以及this指向问题
一.定义函数的方式 //1.function const aaa = function () { } //2.对象字面量中定义函数 const obj = { bbb() { } } //3.ES6中 ...
- JDK1.7 hashMap源码分析
了解HashMap原理之前先了解一下几种数据结构: 1.数组:采用一段连续的内存空间来存储数据.对于指定下标的查找,时间复杂度为O(1),对于给定元素的查找,需要遍历整个数据,时间复杂度为O(n).但 ...
- Hive数据如何同步到MaxCompute之实践讲解
摘要:本次分享主要介绍 Hive数据如何迁移到MaxCompute.MMA(MaxCompute Migration Assist)是一款MaxCompute数据迁移工具,本文将为大家介绍MMA工具的 ...
- Java的Object几个重写的方法
1:toString(); 只是简单的列出对象的状态(也就是重要的实例变量的当前值). package jicheng;public class Animal { public static void ...
- STM32输入捕获TIM2四通道
相比于一通道,原子的例程里因为清了计数时间,所以要对程序进行修改. 记录上升沿后的计数,然后记录下降沿的计数.相减后计算高电平时间,对于定时器中断间隔的边界要分开处理. 这里因为我的接收机时间是1ms ...
- centos7下安装storm步骤
前言 真是后知后觉,最近忙也要学习,把以前丢的都要拾起来.原理懂不懂也把环境搭起来学习. 环境 centos7 jdk 1.8 zookeeper 3.4.13 storm 1.2.2 安装 ...