[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. ...
随机推荐
- IIS 集成模式 导致 AjaxPro 无法正常运行
web.config 配置如下: system.web/httphandlers <httpHandlers> <add verb="POST,GET" path ...
- Mac 永久添加 环境变量方法
在 ~ 目录下 新建 .bash_profile 文件 在文件新增 export PATH="$PATH:/Users/zhangpengchao/tools/flutter/flutter ...
- webpack 中,module、chunk、bundle 的区别(待补充)
项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle
- [转]Python3《机器学习实战》学习笔记(一):k-近邻算法(史诗级干货长文)
转自http://blog.csdn.net/c406495762/article/details/75172850 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 一 简 ...
- OLAP + MDX
基本概念 维度(Dimension):表示数据的属性,一个维度一般会有一个维表(也可能多个),事实表会有一个字段关联维表. 退化维度:有的维度可以没有维度表,因为这种维度比较简单,没有更多属性,没有必 ...
- flask-appbuilder +echarts 展示数据笔记
pip install flask-appbuilder fabmanager create-app cd newapp fabmanager create-admin fabmanager run ...
- [UE4]Scroll Box带滚动条的容器
一.黑边,当可以往下滚动的时候,下边会出现黑边.当可以往上滚动的时候,上边也会出现黑边. Scroll Box.Style.Style:也可以自定义上下左右黑边的样式: 二.Scroll Box. ...
- MII、GMII、RMII、SGMII、XGMII 接口区别
MII即媒体独立接口,也叫介质无关接口.它是IEEE-802.3定义的以太网行业标准.它包括一个数据接口,以及一个MAC和PHY之间的管理接口(图1). 数据接口包括分别用于发送器和接收器的两条独立信 ...
- c# ref与out用法
class Program { static void Main(string[] args) { //普通 : ; ); Console.WriteLine("/*普通:*/") ...
- Maven下CXF的wsdl2java生成客户端代码
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...