[leetcode]Merge Intervals @ Python

原题地址:https://oj.leetcode.com/problems/merge-intervals/

题意:

Given a collection of intervals, merge all overlapping intervals.

For
example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].

解题思路:先将区间按照每个start的值来排序,排好序以后判断一个区间的start值是否处在前一个区间中,如果在前一个区间中,那么合并;如果不在,就将新区间添加。

代码:


# Definition for an interval.
# class Interval:
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e

class Solution:
# @param intervals, a list of Interval
# @return a list of Interval
def merge(self, intervals):
intervals.sort(key = lambda x:x.start)
length=len(intervals)
res=[]
for i in range(length):
if res==[]:
res.append(intervals[i])
else:
size=len(res)
if res[size-1].start<=intervals[i].start<=res[size-1].end:
res[size-1].end=max(intervals[i].end, res[size-1].end)
else:
res.append(intervals[i])
return res

[leetcode]Merge Intervals @ Python

时间: 2025-01-01 20:42:42

[leetcode]Merge Intervals @ Python的相关文章

LeetCode: Merge Intervals [055]

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18].

2015.03.30 LeetCode Merge Intervals 解题记录

今天下午做了一道题.leetcode merge intervals 属于比较难的题目. 首先用collections.sort 给list排序,然后用两个while loop来比较两个interval 的start, end . 从而生成新的interal,再插入到新的list 返回结果. 下面给出自己的代码: /* 50 Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of i

LeetCode: Merge Intervals 解题报告

Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. SOLUTION 1: 1. 先使用Comparator 的匿名类对intervels进行排序. 2. 把Intervals遍历一次,依次一个一个merge到第1个interval. 把第1

[LeetCode] Merge Intervals 排序sort

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. Show Tags Array Sort 这题其实想好思路很好解决,对于框,如果下个框开始在 其中间,则连在一起,否则单独为一个,这需要按start 排序便可以了,因为类中写自定义比较函数比较麻烦,所以一次写了好几个.

LeetCode Merge Intervals

原题链接在这里:https://leetcode.com/problems/merge-intervals/ 首先sort list 中的interval. 然后比较 前一个 interval的end是否 >= 后一个interval 的start, 若是,则合并这两个interval, 用来和下一个比较.若不是,则把这段interval 加到res中去. 通过本题见到了如何改写Comparator, 参见了这篇文章:http://www.blogjava.net/yesjoy/articles

LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。

感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class Solution { public: vector<Interval> merge(vector<In

[Leetcode] merge intervals 合并区间

Given a collection of intervals, merge all overlapping intervals. For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18]. 题意:给定一系列区间,合并重叠区间 思路:其实,个人觉得,题目的例子有一定误导性,以为区间已经按每个区间的start值排好序了.结果,毫无疑问,悲剧了.若是已经排好序,则,只需将当前区间的end值和下一个的start值比较,

[Leetcode][Python]56: Merge Intervals

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 56: Merge Intervalshttps://oj.leetcode.com/problems/merge-intervals/ Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6]

LeetCode --- 56. Merge Intervals

题目链接:Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按