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.

思路


这道题我当时看到之后想的的办法就是将nums中第一个添加进结果集中,然后从nums的第一个开始遍历和和res结果集中进行比较。按照这个思路当时提交之后发现输入的nums并不是有序的,因此先对其进行排序。然后得到排序之后的结果。这次在提交就解决了。 时间复杂度为O(nlogn), 空间复杂度为O(1)。

解决代码


 class Solution(object):
def merge(self, nums):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
if not nums or len(nums) < 2: # 数量小于2个时直接返回结果
return nums
nums.sort() # 排序
res = [nums[0]] # 将nums中第一个元素添加进res中
for li in nums[1:]: # 开始遍历
if res[-1][-1] < li[0]: # 如果当前遍历的范围的起始值大于结果集中的最后一个范围的终止范围值。直接添加
res.append(li)
else:
res[-1][-1] = max(res[-1][-1], li[-1]) # 否则我们选择最大的值作为结束值,并改变res中的值
return res

【LeetCode每天一题】Merge Intervals(合并区间)的更多相关文章

  1. [LeetCode] Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...

  2. [LeetCode] 56 - Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. LeetCode 56. Merge Intervals 合并区间 (C++/Java)

    题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...

  4. 056 Merge Intervals 合并区间

    给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...

  5. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  6. 合并区间 · Merge Intervals & 插入区间 · Insert Interval

    [抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...

  7. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  8. [leetcode]56. Merge Intervals归并区间

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  9. [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

随机推荐

  1. vue2.0 技巧汇总

    /** * Created by */ export default { trim: (str) => { //删除左右两端的空格 return str.replace(/(^\s*)|(\s* ...

  2. x-www-form-urlencoded与multipart/form-data区别

    转载声明: http://blog.chinaunix.net/uid-7210505-id-329700.html 在Flex中,UrlRequest中的contentType默认值为 applic ...

  3. python 操作Sqlite

    Python sqlite 官方文档: https://docs.python.org/2/library/sqlite3.html 1. 连接 #!/usr/bin/python3 import s ...

  4. JAVA—API和SPI概念

    JAVA—API和SPI概念 目录 概念 JDBC实例 自己实现一个SPI 总结 概念英文: What is the difference between Service Provider Inter ...

  5. Linux iptables原理和使用

    1.原理 iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵 ...

  6. hive建表报错:Specified key was too long; max key length is 767 bytes,hadoophive

    情况描述 Hive建表时报错,元数据存储在mysql中. 报错信息 如下: FAILED: Execution Error, bytes com.mysql.jdbc.exceptions.jdbc4 ...

  7. swiper4自动轮播切换手动触碰后停止踩坑——属性disableOnInteraction

    swiper4轮播设置autoplay自动切换后,即默认设置: <script> var mySwiper = new Swiper('.swiper-container', { auto ...

  8. monit检测语法

    1.存在性检测 功能:检测文件或者服务不存在时进行相应的动作,默认是重启 语法: IF [DOES] NOT EXIST [[<X>] <Y> CYCLES]    THEN ...

  9. Javascript 变量、函数的声明

    javascript变量 全局变量和局部变量    按照变量的作用域来区分,和大多数编程语言类似,javascript变量也分为全局变量和局部变量.全局变量的作用域是整个js文件,而局部变量的作用域是 ...

  10. android 常用框架

    网络框架:okhttp.volley.android-async-http图片框架:Picasso.Fresco.Glide.Android-Universal-Image-Loader缓存框架:Di ...