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对象集合,然后对重叠的区域 ...
随机推荐
- RabbitMQ入门教程(九):首部交换机Headers
原文:RabbitMQ入门教程(九):首部交换机Headers 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...
- setTimeout定时器
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 为Xcode配置Git和Github
Xcode.Git和Github是三个伟大的编程工具.本文记录一下如何在Xcode中使用Git作为源代码控制工具,以及如何将本地的Git仓库和远程Github上的仓库集成起来. 1. 如何为新建的Xc ...
- 学习-Pytest(三)setup/teardown
1. 用例运行级别 模块级(setup_module/teardown_module)开始于模块始末,全局的 函数级(setup_function/teardown_function)只对函数用例生效 ...
- DP+滚动数组 || [Usaco2007 Nov]Telephone Wire 架设电话线 || BZOJ 1705 || Luogu P2885
本来是懒得写题解的…想想还是要勤发题解和学习笔记…然后就滚过来写题解了. 题面:[USACO07NOV]电话线Telephone Wire 题解: F[ i ][ j ] 表示前 i 根电线杆,第 i ...
- MySQL中添加、修改、删除约束
https://blog.csdn.net/dz77dz/article/details/82119000 主要包含的约束: 非空.唯一.check.not null.默认值.主键.外键
- java课堂作业4
第一题 字符串加密问题 1.程序设计思想 读入字符串,然后获取其长度,利用charAt()获取每个位置字符并且对字符加3实现加密处理,并存入新字符串中.如果遇到xyz则减26存入. 2.程序流程图 3 ...
- c语言日志打印
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdarg.h> ...
- big.js常用方法
官网api:http://mikemcl.github.io/big.js/ +:minus.minus(n) ⇒ Big %:mod.mod(n) ⇒ Big -:plus.plus(n) ⇒ B ...
- JAVA泛型里面各值代表的意义
Java泛型中的标记符含义: E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number ...