LeetCode 56. 56. Merge Intervals 20170508

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].

题目大意:给出一个区间的集合,将重叠的区间合并

解题思路:

1、将集合内的区间按照区间的起始位置的大小从小到大排序

2、用一个新的list来保存合并后的结果,先放入起始位置最小的区间

3、接下来每次取原集合中起始位置最小的区间a,跟新的list中起始位置最大的区间b对比,如果b的结束位置比a的起始位置要大的话,说明两者有重叠,

所以接下来只要对比a和b的结束位置谁大,就把大的值作为b的新的结束位置。如果b的结束位置比a的起始位置小的话,说明两者没有重叠,则直接把a添加到新的list中。

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

class Solution(object):
  def merge(self, intervals):
  """
  :type intervals: List[Interval]
  :rtype: List[Interval]
  """
  newcollection = []
  if len(intervals) == 0:
    return newcollection
  intervals.sort(key=lambda x: x.start)
  newcollection.append(intervals[0])
  for interval in intervals[1:]:
    length = len(newcollection)
    prev = newcollection[-1]
    if newcollection[length - 1].end >= interval.start:
      newcollection[length - 1].end = max(newcollection[length - 1].end, interval.end)
    else:
      newcollection.append(interval)
    return newcollection

时间: 2024-08-02 20:01:09

LeetCode 56. 56. Merge Intervals 20170508的相关文章

leetcode || 56、 Merge Intervals

problem: 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]. Hide Tags Array Sort 题意:给定数组区间,合并有覆盖或者相邻的区间 thinking: (1)一開始我想到用hash table的方法,开一个总区间跨度的数组.对于有区间覆盖的数

【LeetCode】Merge Intervals 题解 利用Comparator进行排序

题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int end; * Interval() { start = 0; end = 0; } * Interval(int s, int e) { start = s; end = e; } * } * 题目:LeetCode 第56题 Merge Intervals 区间合并给定一个区间的集合,将相邻区间之间

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]. 这道题的要求是将给定的一组间隔中有重叠的进行合并. 将间隔合并,首先要找到相邻的间隔,然后看其是否有重叠,如果有,就进行合并. 因此,首先考虑对数组排序.排序的时候,只需要按

[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]

56. Merge Intervals - LeetCode

Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个范围内,如果在且结束坐标大于上一个坐标就合并 Java实现: public List<Interval> merge(List<Interval> intervals) { if (intervals == null || intervals.size() == 0) return i

LeetCode开心刷题四十二天——56. Merge Intervals

56. Merge Intervals Medium 2509194FavoriteShare 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, mer

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]. 思路:题意很明确,首先对各区间按开始来排序,最后遍历,如果前面和后面的区间有重合,合并. 具体代码: /** * Definition for an interval. * publi

No.56 Merge Intervals

No.56 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]. 法一:直接复用之前写的insert函数,依次将区间段插入到结果集中 1 #include "stdafx.h" 2 #include <vector> 3

56. Merge Intervals 57. Insert Interval *HARD*

1. Merge 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]. /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0)