题目:

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.

分析:

给定一个区间的集合,合并所有重复的区间。

也就是将重复的区间合并成一个大的区间,例如[1,5]和[3,7]将会合并成[1,7],做法就是先将区间按左端点值由小到大排序,然后遍历区间,当后一个区间的左端点值大于它前一个区间的右端点值,代表两个区间无重复部分,将前一个加入到结果中,反之,代表有重复的,需要合并两个区间,由于区间的顺序由左端点值排序,则合并后的区间的左端点值等于前一个区间的左端点值,而右端点值则在两个区间的右端点值中取最大值,因为有可能发生包含这种情况,比如[1,7]和[3,5]这种,合并后就还是[1,7]。

程序:

C++

class Solution {
public:
vector<vector<int>> merge(vector<vector<int>>& intervals) {
if(intervals.size() == )
return {};
vector<vector<int>> res;
sort(intervals.begin(), intervals.end(), cmp());
for(auto interval:intervals){
if(res.empty())
res.push_back(interval);
else if(res.back()[] < interval[])
res.push_back(interval);
else{
auto t = res.back();
res.pop_back();
res.push_back({t[], max(t[], interval[])});
}
}
return res;
}
struct cmp {
bool operator() (const vector<int>& A, const vector<int>& B) {
return A[] < B[];
}
};
};

Java

class Solution {
public int[][] merge(int[][] intervals) {
if(intervals.length == 0)
return intervals;
Arrays.sort(intervals, (o1, o2) -> {
return o1[0] - o2[0];
});
LinkedList<int[]> res = new LinkedList<>();
for(int[] interval:intervals){
if(res.isEmpty())
res.add(interval);
else if(res.getLast()[1] < interval[0])
res.add(interval);
else{
int[] t = res.removeLast();
res.add(new int[]{t[0], Math.max(t[1], interval[1])});
}
}
return res.toArray(new int[res.size()][]);
}
}

LeetCode 56. Merge Intervals 合并区间 (C++/Java)的更多相关文章

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

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

  2. [leetcode]56. Merge Intervals归并区间

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

  3. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  4. LeetCode 56. Merge Intervals (合并区间)

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

  5. [LeetCode] Merge Intervals 合并区间

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

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

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

  7. 056 Merge Intervals 合并区间

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

  8. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

  9. Leetcode56. Merge Intervals合并区间

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

随机推荐

  1. ros机器人之动作(二)

    前面我们实现了动作的定义,接下来实现动作的功能 实现一个基本的动作服务器 准备好所需的动作定义后就可以开始编写代码了.动作和话题一样,都是使用回调机制,即回调函数会在收到消息时被唤醒和调用. 例:si ...

  2. winsocket入门学习

    参考资料:http://c.biancheng.net/cpp/socket/ http://www.winsocketdotnetworkprogramming.com/ socket 是" ...

  3. 解决dotnet错误 System.InvalidOperationException Message=Unable to configure HTTPS endpoint. No server certificate was specified, and the default developer certificate could not be found.

    开始=>设置=>manage user certificats  (管理用户证书),里面所有的.net core的全部删除 然后控制台执行: dotnet dev-certs https ...

  4. python dict 中的中文处理

    dict1 = {'中':'国 '} print dict1 ##{'\xc3\xa4\xc2\xb8\xc2\xad': '\xc3\xa5\xc2\x9b\xc2\xbd'} import jso ...

  5. Codeforces_839

    A.每天更新判断. #include<bits/stdc++.h> using namespace std; ]; int main() { ios::sync_with_stdio(); ...

  6. WeChall_Encodings: URL (Training, Encoding)

    Your task is to decode the following: %59%69%70%70%65%68%21%20%59%6F%75%72%20%55%52%4C%20%69%73%20%6 ...

  7. 9.3.1 map端连接- DistributedCache分布式缓存小数据集

    1.1.1         map端连接- DistributedCache分布式缓存小数据集 当一个数据集非常小时,可以将小数据集发送到每个节点,节点缓存到内存中,这个数据集称为边数据.用map函数 ...

  8. 《C# 爬虫 破境之道》:第二境 爬虫应用 — 第四节:小说网站采集

    之前的章节,我们陆续的介绍了使用C#制作爬虫的基础知识,而且现在也应该比较了解如何制作一只简单的Web爬虫了. 本节,我们来做一个完整的爬虫系统,将之前的零散的东西串联起来,可以作为一个爬虫项目运作流 ...

  9. Mysql的基本查询语句

    聚集函数 mysql有5个聚集函数,分别是AVG,MAX,MIN,SUM,COUNT. 分组 分组的使用group by作为关键字,having作为条件关键字. having和where的区别:1.w ...

  10. Ambari HDP 下 SPARK2 与 Phoenix 整合

    1.环境说明 操作系统 CentOS Linux release 7.4.1708 (Core) Ambari 2.6.x HDP 2.6.3.0 Spark 2.x Phoenix 4.10.0-H ...