[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. ...
随机推荐
- C# 左右补零
//不够4位补零 public static string addZero(int val) { string str = val + ""; int strLen = str.L ...
- Excel技巧--实现交叉查询
如上图,要实现某个地区和某个产品的销售额查询显示.可以使用Match和Index函数的使用来实现: 1.产品名称和城市栏,制作成列表可选:使用“数据”-->“数据验证”的方法. 2.先在旁边空位 ...
- Beautiful Soup库基础用法(爬虫)
初识Beautiful Soup 官方文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc/# 中文文档:https://www.crumm ...
- 接口测试工具SoapUI Pro5.1.2基本使用20150920
soapui是接口测试工具,最近因为要做接口测试,使用了下,现在和大家分享下: 工具安装很简单,就不说了,直接说使用,先什么都不说,照着操作一遍,我们拿天气预报的webserver来实战: 主要包括: ...
- E212: Can't open file for writing Press ENTER or type command to continue
E212: Can't open file for writing Press ENTER or type command to continue 出现这个错误的原因可能有两个: 1.当前用户的权限不 ...
- 黄聪:解决Bootstrap模态框(modal)弹出后页面跑到顶部的办法
bootstrap 3.1.1 版本解决方案: body.modal-open { position: absolute !important; }
- Python【每日一问】04
问:a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],求出列表a中所有奇数并构造新列表 答: 利用列表的元素下标遍历列表 a = [1, 2, 3, 4, 5, 6, 7, 8 ...
- VLAN中继协议
VTP(VLAN Trunking Protocol):是VLAN中继协议,也被称为虚拟局域网干道协议.作用是十几台交换机在企业网中,配置VLAN工作量大,使用VTP协议,把一台交换机配置成VTP S ...
- 5-安装sqoop
1.解压,修改权限 sudo tar -zvxf sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz -C /opt/app/ sudo chown -R hadoo ...
- python第三方库Requests的基本使用
Requests 是用python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...