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的方法,开一个总区间跨度的数组。对于有区间覆盖的数组区间置为true。没被覆盖的数组区间置为false,最后将true区间的起点和终点作为区间输出就可以。

思路简单。可是,我忽略一个问题:区间跨度是不定的,所以要开的数组大小有可能非常大。提交也显示:Memory
Limit Exceeded

(2)换一种方法。排序法。

能够直接对vector<Interval> 数组排序,要重载compare函数。也能够使用multimap<int, int>,注意不是map

code:

排序法: Accepted

class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        vector<Interval> ret;
        multimap<int,int> map_intervals;
        if(intervals.size()==0)
            return ret;
        if(intervals.size()==1)
            return intervals;
        for(vector<Interval>::iterator it=intervals.begin();it!=intervals.end();it++)
            map_intervals.insert(make_pair((*it).start,(*it).end));
        multimap<int, int>::iterator p=map_intervals.begin();
        Interval tmp(p->first,p->second);
        for(multimap<int, int>::iterator k=++p;k!=map_intervals.end();k++)
        {
            if(k->first<=tmp.end)
                tmp.end=max(tmp.end,k->second);
            else
            {
                ret.push_back(tmp);
                tmp.start=k->first;
                tmp.end=k->second;
            }
        }
        ret.push_back(tmp);
        return ret;
    }
};

hash table 法:Memory Limit Exceeded

class Solution {
public:
    vector<Interval> merge(vector<Interval> &intervals) {
        vector<Interval> ret;
        int first=INT_MAX, last=INT_MIN;
        for(vector<Interval>::iterator tmp=intervals.begin();tmp!=intervals.end();tmp++)
        {
            first=min((*tmp).start,first);
            last=max((*tmp).end,last);
        }
        int count=last-first+1;
        bool *a = new bool[count];
        memset(a,false,sizeof(bool)*count);
        for(vector<Interval>::iterator it=intervals.begin();it!=intervals.end();it++)
        {
            int num=(*it).end-(*it).start+1;
            memset(a+(*it).start,true,sizeof(bool)*num);
        }
        int interval_start=0,interval_end=0;
        while(interval_end<count)
        {
            interval_end=interval_start;
            int index=0;
            while(interval_start+index<count && a[index])
                index++;
            interval_end+=index-1;
            Interval tmp_interval(interval_start,interval_end);
            ret.push_back(tmp_interval);
            if(interval_end<count)
            {
                int loc =interval_end+1;
                while(loc<count && (!a[loc]))
                    loc++;
                interval_start=loc;
            }
        }

    }
};
时间: 2024-10-13 18:45:49

leetcode || 56、 Merge Intervals的相关文章

leetcode || 88、Merge Sorted Array

problem: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A an

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

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

[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 解题报告

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