题目:

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. 使用c++标准IO库实现txt文本文件的读与写操作

    练习c++primer中关于输入输出流的操作. 任务是从固定格式的forreading.txt文档中读取相应的数据,转存到forwriting.txt中去. forreading.txt 格式如下: ...

  2. Redis异常 | DENIED Redis is running in protected mode because protected mode is enabled

    背景 今天重新搭了个redis环境,用简单的代码去测试下是否正常, @RunWith(SpringRunner.class) @SpringBootTest public class Springbo ...

  3. Dockers 部署 MongoDB + mongo-express

    1. 拉取 Mongo 镜像 docker pull mongo: 2.  运行镜像 docker run -d --name mongodb --volume /usr/local/mongodat ...

  4. vim 实用快捷键

    删除当前行:dd 删除上一行:dj 删除下一行:dk 拷贝当前行:yy 交换当前行和其下一行 交换当前字符和其后的一个字符 剪切当前字符:x 剪切当前光标开始向后三个字符:3x 撤销最近一次修改:u ...

  5. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  6. koa中间执行机制

    start 基于 koa 2.11 按以下流程分析: const Koa = require('koa'); const app = new Koa(); const one = (ctx, next ...

  7. Node.js实战--资源压缩与zlib模块

    Blog:<NodeJS模块研究 - zlib> Github:https://github.com/dongyuanxin/blog nodejs 的 zlib 模块提供了资源压缩功能. ...

  8. SQL Server 2012 安装完成后,无法通过 sa账号登录

    1.打开 SQL server  configuration manager 2.打开 SQLserver 网络配置 打开 SQLSERVER的协议 3.右击 TCP/IP协议,选择 IPALL ,在 ...

  9. ccf

    import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; public class MST { pu ...

  10. CCF_201612-4_交通规划

    http://115.28.138.223/view.page?gpid=T44 好像也没想象中的那么难,没办法,当初连个优先队列dij都不会写= = 在优先队列dij算法上加上相等的时候的处理就可以 ...