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. 【计算机网络】OSI七层模型图解

    1.物理层 建立.维护.断开物理连接.(由底层网络定义协议) 2.数据链路层 建立逻辑连接.进行硬件地址寻址.差错校验等功能.(由底层网络定义协议) 将比特组合成字节进而组合成帧,用MAC地址访问介质 ...

  2. Java编程的逻辑 (94) - 组合式异步编程

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  3. Linux操作系统配置Go语言编程环境

    之前一直在windows下写Go,现在工作环境切换至Linux下,因此写下此文,记录安装Go环境的过程. 操作系统:CentOS7.5 一.安装步骤 1,下载Go语言安装包 yum install g ...

  4. Python学习笔记——发邮件

    参考:Python3实现163邮箱SMTP发送邮件 1.首先需要注册一个网易的邮箱,开启smtp服务,并使用其授权码 2.发送邮件的Python脚本 #!/usr/bin/python # -*- c ...

  5. [hive] hive 内部表和外部表

    1.内部表 hive (test1)> create table com_inner_person(id int,name string,age int,ctime timestamp) row ...

  6. linux查找并替换命令

     find ./ -maxdepth 3 -type f -name "*Makefile"  |xargs sed -i "s/CXX = g++/CXX = ccac ...

  7. Android的Base64的坑

    Base64.encodeToString加密后一直和Apache的对不上,多了换行符,最后使用了NO_WRAP就好了 Base64.encodeToString(src, Base64.URL_SA ...

  8. Sublime Text 3 3143 注册码和遇到的PyV8无法下载问题

    packagecontrol官网:https://packagecontrol.io/installation sublime安装之后,安装Package Control管理插件. sublime3 ...

  9. An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later.

    刚装完 PHP.Nginx,准备跑下 phpMyAdmin 程序,结果报以下错误: An error occurred. Sorry, the page you are looking for is ...

  10. git使用——推送本地文件到远程仓库

    捣鼓了一下午之后总结如下:   1.首先可以照着这个链接里面博主给出的详细方法进行操作和配置: http://www.open-open.com/lib/view/open1454507333214. ...