Merge Intervals -- leetcode

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), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */
class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        if (intervals.size() <= 1)
            return intervals;

        sort(intervals.begin(), intervals.end(), [](const Interval &a, const Interval &b) {return a.start < b.start;});

        size_t j = 0;
        for (size_t i = j+1; i<intervals.size(); i++) {
            if (intervals[j].end >= intervals[i].start)
                intervals[j].end = max(intervals[j].end, intervals[i].end);
            else
                intervals[++j] = intervals[i];
        }

        intervals.erase(intervals.begin()+j+1, intervals.end());
        return intervals;
    }
};

该算法在leetcode上实行执行时间为20ms。

基本思路:

1.利用自定的比较函数进行排序,以达到按start进行升序。

2. 利用remove-duplicates-from-sorted-array的思路进行归并

时间: 2024-10-17 15:31:48

Merge Intervals -- leetcode的相关文章

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

Merge Intervals——LeetCode

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是否有overlap,没有的话,可以输出当前这个区间,有的话把后一个的end设置为

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】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][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: 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 [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].

[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值是否处在前一个区间

【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大小排序,然后遍历排序后的数组,用last记录前一个区间,如果遍历的当前区间可以和last合并,就把它合并到last里面:否则就把last放到answer list中,并且更新last