[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. ...
随机推荐
- Centos7安装mysql5.6.29shell脚本
创建脚本mysql.sh,直接运行sh mysql.sh #!/bin/bash if [ -d /software ] ;then cd /software else mkdir /software ...
- webpack 中,module、chunk、bundle 的区别(待补充)
项目 区别 module 是开发中的单个模块 chunk 中文意思是"块",是指 webpack 在进行模块依赖分析的时候,代码分割出来的代码块 bundle
- 1.1.15 word调整文字与下划线之间的间距
先请按CTRL+U快捷键,或点击“下划线”按钮,然后输入一个空格,再输入文字“下划线间距”,在文字的尾部再添加一个空格.选中文字内容(注意不要选中首尾的空格),单击菜单“格式”→“字体”,在“字体”设 ...
- Optaplanner - 从探究示例中的hello world,初步认识规划引擎的运行步骤。
上一篇我们成功以把Opotaplanner规划引擎下载回来,并把它的示例运行起来,简单解析了一下它的Cloud balance示例.这一篇我们这些示例的源代码导入到Eclipse中,看看它在后台是怎么 ...
- G2( bizCharts ) React 绘制混合图例
G2( bizCharts ) React 绘制混合图例, // data-set 可以按需引入,除此之外不要引入别的包 import React from 'react'; import { Cha ...
- awk字符串操作(字符串链接、传入传出shell变量)
1.awk基础 awk的环境变量及其意义 https://blog.csdn.net/snowpay/article/details/52451718 linux awk命令详解 https:// ...
- 2017-07-06 eclipse在线安装SVN1.9插件
1,百度搜索subeclipse,点击第一个: 2,官网说,文档已移动到github wiki上: 3,打开github wiki,复制最新发布版本地址: 4,在eclipse里面,打开help-&g ...
- springBoot 整合mybaits 逆向工程
pom.xml文件中增加配置项 <build> <plugins> <plugin> <groupId>org.springframework.boot ...
- 变式配置简介 VARIANT CONFIGURATION
变式物料类型KMAT; ITEM CATEGORY GROUP: main item 0002 sub item 0004 strategy group:25 requirement type: ke ...
- VS2015密匙--VS2015打开丢失msvcp140.dll--cannot find one or more components ,please reinstall the application
win7旗舰版 64位 + vs2015 专业版 1.安装VS2015过程中可能需要用到的VS2015专业版钥匙:(测试,可用) HMGNV-WCYXV-X7G9W-YCX63-B98R2 2.VS2 ...