[leetcode]56. Merge Intervals归并区间
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].
题意:
给定一些区间,将重叠部分合并。
思路:
将原interval集合里,给定区间按照start开头值的从小到大排序
建一个新的interval集合
遍历原interval集合的每个区间,
若cur.start > pre.end 则说明没有重叠,扔到新的interval集合去。
否则,有重叠,则更新之前区间.end的长度
代码:
class Solution {
public List<Interval> merge(List<Interval> intervals) {
Collections.sort(intervals, (o1, o2)-> o1.start - o2.start);
List<Interval> result = new ArrayList<>();
Interval pre = null;
for(Interval cur : intervals){
if(pre == null || cur.start > pre.end){
result.add(cur);
pre = cur;
}else{
pre.end = Math.max(pre.end, cur.end);
}
}
return result;
}
}
[leetcode]56. Merge Intervals归并区间的更多相关文章
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 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 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- Leetcode#56 Merge Intervals
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
- [LeetCode] 56. Merge Intervals(vector sort)
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
随机推荐
- Unity在WPF中的应用
1. 本文的实现类继承于IRepository using System; using System.Linq; using System.Linq.Expressions; using Zhang. ...
- django路由系统URLS
usrls: from django.contrib import admin from django.urls import path from cmbd import views from dja ...
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- Hibernate复习
第一天 Hibernate是一个持久层的ORM框架.两个配置文件, 类名.hbm.xml类的属性和表的列对应 hibernate.cfg.xml核心配置文件 Hibernate相关API: Confi ...
- 饥饿的牛(dp一维最大覆盖)
问题 H: 饥饿的牛 时间限制: 1 Sec 内存限制: 128 MB提交: 12 解决: 12[提交][状态][讨论版][命题人:外部导入][Edit] [TestData] [同步数据] 题目 ...
- vscode, cmake编译多个C++文件
目的是利用vscode及相关插件编译多个C++文件. 我已经装好cmake和mingw并且将它们的路径添加到系统变量path中了. vscode装上如下几个插件: 点击vscode左上角 文件-& ...
- CSS之display
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IIC时序详解
Verilog IIC通信实验笔记 Write by Gianttank 我实验的是 AT24C08的单字节读,单字节写,页读和页写,在高于3.3V系统中他的通信速率最高400KHZ的,我实验里用的是 ...
- js 常用代码片段
一.预加载图像 如果你的网页中需要使用大量初始不可见的(例如,悬停的)图像,那么可以预加载这些图像. function preloadImages(){ for(var i=0;i<argume ...
- vs与linux的交叉编译环境搭建
很久之前就想写一个linux服务器,但是对linux的vim编译工具又不是很熟,只能在win环境下写好代码拷贝到linux环境下编译运行,现在VS出了一个插件可以对linux代码远程在linux环境下 ...