leetcode-mid-sorting and searching - 56 Merge Intervals
mycode
出现的问题:比如最后一个元素是【1,10】,1小于前面所有元素的最小值,10大于前面所有元素的最大值,而我最开始的思路只考虑了相邻
参考:
思路:如果我只考虑相邻,必须先将list排序,由于key只有一个,所以还要在循环的时候考虑第二个元素
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[List[int]]
:rtype: List[List[int]]
"""
intervals.sort(key = lambda x:x[])
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
size=len(res)
if res[size-][] <= intervals[i][] <=res[size-][-]:
res[size-][-] = max(intervals[i][-], res[size-][-])
else:
res.append(intervals[i])
return res
leetcode-mid-sorting and searching - 56 Merge Intervals的更多相关文章
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
- 刷题56. Merge Intervals
一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 56. Merge Intervals 57. Insert Interval *HARD*
1. Merge Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[ ...
- 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
一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
随机推荐
- .net core 2.2.0 SOAP踩坑
首先确认下面几个程序集是最新版本: <PackageReference Include="System.ServiceModel.Http" Version="4. ...
- 097、如何实现Service伸缩?(Swarm04)
参考https://www.cnblogs.com/CloudMan6/p/7885667.html 上一节部署了只有一个副本的Service,不过对于web服务,我们通常会运行多个实例,这样可以 ...
- vue学习【番外篇】vue-cli脚手架的安装
大家好,我是一叶,今天和大家分享的是vue-cli脚手架的安装,关于vue-cli的优点,我就不赘述了. 一.检查安装node 安装vue-cli之前,先检查node是否安装.win+R,输入cmd打 ...
- iOS 跳转系统设置界面
iOS 跳转系统设置界面 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Pri ...
- java软件设计模式只单例设计模式
概述 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计 ...
- Nginx进行UDP的负载均衡
准备工作: 服务器1:192.168.33.102 搭建nginx服务,作为反向代理的中转站 服务器2:192.168.33.103 nginx要反向代理的服务器 一.在服务器1上搭建n ...
- 【转】ubuntu或linux网卡配置/etc/network/interfaces
转自:https://www.cnblogs.com/qiuxiangmuyu/p/6343841.html 青蛙准备写一个系列文章,介绍一些Debian/Ubuntu里面常用的配置文件.当然,Lin ...
- navicat修改表的主键自增长报错
这周自己在构思一个项目的表的设计,由于是第一次,所以走了很多弯路,也遇到了几个问题,这里暂时贴上来. 我用PowerDesign设计出一部分关联表的ER图之后,导出了sql文件之后用navicat导入 ...
- 牛客练习赛26 D xor序列 (线性基)
链接:https://ac.nowcoder.com/acm/contest/180/D 来源:牛客网 xor序列 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他 ...
- 写一个函数,对于一个给定的整数,如果它的二进制模式从正向看和反向看是一样的,那么返回true;
写一个函数,对于一个给定的整数,如果它的二进制模式从正向看和反向看是一样的,那么返回true:也就是实现这样一个函数boolean isPalindrome(int x); 分析一下,该题目主要是通过 ...