【LeetCode每天一题】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].
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(合并区间)的更多相关文章
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [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] ...
- 056 Merge Intervals 合并区间
给出一个区间的集合, 请合并所有重叠的区间.示例:给出 [1,3],[2,6],[8,10],[15,18],返回 [1,6],[8,10],[15,18].详见:https://leetcode.c ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- 合并区间 · Merge Intervals & 插入区间 · Insert Interval
[抄题]: 给出若干闭合区间,合并所有重叠的部分. 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8, 10] ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- [LeetCode] Data Stream as Disjoint Intervals 分离区间的数据流
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
随机推荐
- 深度学习基础(CNN详解以及训练过程1)
深度学习是一个框架,包含多个重要算法: Convolutional Neural Networks(CNN)卷积神经网络 AutoEncoder自动编码器 Sparse Coding稀疏编码 Rest ...
- Excel公式笔记
跨表,查找相同的数据,的其他列的值 =index(sheet2!$c$:$c$,match(sheet1!$b$,sheet2!$d$:$d$,)) 备注: =index(读哪里的数据(大范围), m ...
- AOP 技术原理——代理模式全面总结
前言 非常重要的一个设计模式,也很常见,很多框架都有它的影子.定义就不多说了.两点: 1.为其它对象提供一个代理服务,间接控制对这个对象的访问,联想 Spring 事务机制,在合适的方法上加个 tra ...
- 【转】IT大牛博客
原文:http://blog.csdn.net/qq1175421841/article/details/49384841 首届中国最受欢迎50大技术博客获奖名单如下: 第一名:李会军 http:/ ...
- 安装Inotify-tools
Inotify-tools 可以帮助Liunx快速实现文件同步. 1. 安装Inotify-tools mkdir /soft chmod /soft cd /soft wget http://git ...
- Unity长连接
http://blog.csdn.net/claine/article/details/52374546
- Debian/Ubuntu pip default install to $HOME/.local
pip default install to $HOME/.local on Debian/Ubuntu After pip 8.1.1-2 on Debian or Ubuntu you can p ...
- MySQL之SQL注入
1.SQL注入? 通过SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. 2.防止SQL注入,我们需要注意以下几个要点: ①永远不要信任用户的输入 ...
- python数据分析及展示(一)
一.IDE选择 Anaconda软件:开源免费,https://www.anaconda.com下载,根据系统进行安装.由于下载速度慢,可以去清华大学开源软件镜像站下载. Spyder软件设置:Too ...
- React 性能调优总结
React 性能调优总结 首先要说一个库: why-did-you-update, 地址:why-did-you-update, 利用这个库可以在页面上快速看到多余渲染的问题: 因为多数情况下我们在R ...