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(合并序列)的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  2. [LeetCode] 56 - Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  4. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  5. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  6. merge intervals(合并间隔)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  7. 【LeetCode每天一题】Merge Intervals(合并区间)

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  8. 【leetcode】Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  9. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. simple_pt时遇到的问题

    elf.c:30:18: fatal error: gelf.h: No such file or directory 安装libelf-dev 遇到找不到ldwarf apt-cache  sear ...

  2. 免费资源(CDN,顶级域名)汇集

    CloudFlare:免费CDN,需要将域名指向到cloudflare服务器.付费的可以使用二级域名 https://www.cloudflare.com/ Freenom:freenom会提供免费提 ...

  3. java String练习题

    package java07; /* 题目: 定义一个方法,把数组{1,2,3}按照指定格式拼接成一个字符串,格式参照如下:[word1#word2#word3] 思路: 1.首先准备一个int[]数 ...

  4. windows平台搭建Mongo数据库复制集(类似集群)(一)

    Replica  Sets(复制集)是在mongodDB1.6版本开始新增的功能,它可以实现故障自动切换和自动修复功能成员节点的功能,各个DB之间的数据完全一致,大大降低了单点故障的风险. [] 以上 ...

  5. BZOJ2280 [Poi2011]Plot 二分+倍增+最小圆覆盖

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=2280 https://loj.ac/problem/2159 题解 显然对于一段的 \(q_i ...

  6. php 连接数据库 Warning: mysqli_connect(): (HY000/2002): No such file or directory in

    1.错误代码 //主机名 $db_host = 'localhost'; //用户名 $db_user = 'jaing'; //密码 $db_password = '1'; //数据库名 $db_n ...

  7. 10.Servlet简单介绍

    1.什么是Servlet * Servlet是javaweb的三大组件之一,它属于动态资源.Servlet的作用是处理请求,服务器会把接收到的请求交给Servlet来处理,在Servlet种通常需要: ...

  8. abstractmethod

    a class with an abstract method cannot be instantiated (that is, we cannot create an instance by cal ...

  9. Linux用户登出之后保持后台进程(nohup)

    使用&可以将进程置于后台,但是用户从Shell登出之后,进程会自动结束.想要在登出之后保持进程运行,就要结合nohup命令使用. 例如: nohup find -size +100k > ...

  10. HDU 6090 Rikka with Graph —— 2017 Multi-University Training 5

    Rikka with Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...