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的更多相关文章

  1. [Leetcode][Python]56: Merge Intervals

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...

  2. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  3. 56. Merge Intervals - LeetCode

    Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...

  4. 刷题56. Merge Intervals

    一.题目说明 题目是56. Merge Intervals,给定一列区间的集合,归并重叠区域. 二.我的做法 这个题目不难,先对intervals排序,然后取下一个集合,如果cur[0]>res ...

  5. 【LeetCode】56. Merge Intervals

    Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given  ...

  6. 56. Merge Intervals 57. Insert Interval *HARD*

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

  7. LeetCode 56. Merge Intervals (合并区间)

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

  8. 【一天一道LeetCode】#56. Merge Intervals

    一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...

  9. LeetCode: 56. Merge Intervals(Medium)

    1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...

随机推荐

  1. openstack docker build error

    1. _proto_tcp = socket.getprotobyname('tcp') OSError: protocol not found you should have a /etc/prot ...

  2. Win7 MongoDB可视化工具Robo 3T 1.2.1(robomongo)的安装使用

    软件版本: Robo 3T 1.2.1 下载网址: https://robomongo.org/campaign 进入robomongo官网,点击download,进入下载页面 这里选择下载 Robo ...

  3. java中的进制转换

    java中的进制转换及转换函数 转自:https://blog.csdn.net/V0218/article/details/74945203 Java的进制转换 进制转换原理 十进制 转 二进制: ...

  4. 2018.09.07 最新cocoapods安装流程

    这篇写在简书了,就不费力气搬了,给简书留一篇. https://www.jianshu.com/p/13bbbf804b71

  5. mysql5和mysql8连接数据库的配置

    mysql5: mysql8: db.properties jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3 ...

  6. 018-zabbix_api

    Zabbix API 简介 Zabbix API 开始扮演着越来越重要的角色,尤其是在集成第三方软件和自动化日常任务时. 很难想象管理数千台服务器而没有自动化是多么的困难. Zabbix API 为批 ...

  7. imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable

    错误: imagecreatefromstring(): Empty string or invalid image 或者 imagesx() expects parameter 1 to be re ...

  8. Pythoy修炼之路

    路漫漫其修远兮,吾将上下而求索... 编程语言众多,徘徊在C.C#.Java.PHP等十字路口,看到哪个领域大牛的成功,便想打开那扇窗,反反复复,犹豫不决,终无所获. 个人认为选择一门语言,要根据自身 ...

  9. chrome 开发者工具 - local overrides

    使用chrome 作为本地网络服务 chrome 65+ 新功能, 使用我们自己的本地资源覆盖网页所使用的资源,可以使用本地css文件覆盖网页的css文件,修改样式. 类似的,使用DevTools的工 ...

  10. HDU-6669-Game(模拟,贪心)

    链接: https://vjudge.net/problem/HDU-6669 题意: 度度熊在玩一个好玩的游戏. 游戏的主人公站在一根数轴上,他可以在数轴上任意移动,对于每次移动,他可以选择往左或往 ...