Merge Intervals 运行比较快

class Solution {
public:
static bool cmp(Interval &a,Interval &b)
{
    return a.start<b.start;
}
    vector<Interval> merge(vector<Interval>& intervals) {
        vector<Interval> res;
        if(intervals.size()==0)return res;
        sort(intervals.begin(),intervals.end(),cmp);
        int i=1;
        int first=0;
        int j=first;
        Interval tmpin;
        while(i<intervals.size()){
            //第一种情况比如:[1,2] [3,4]
        if(intervals[i].start>intervals[j].end){tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin);
            first=i;
            j=first;
            i=i+1;
        }

        //第二种  比如: [2,6] [3,7]
        else if(intervals[i].start<=intervals[j].end&&intervals[i].end>intervals[j].end)
        {
            i++;
            j=i-1;
        }
        //第三种  比如: [2,6] [1,3]
        else
        {
           i++;
        }

        }
        if(intervals[j].end>=intervals[i-1].end) {tmpin.start=intervals[first].start;tmpin.end=intervals[j].end;res.push_back(tmpin);}
        else{tmpin.start=intervals[first].start;tmpin.end=intervals[i-1].end;res.push_back(tmpin);}
        return res;
    }
};
时间: 2024-10-06 00:16:16

Merge Intervals 运行比较快的相关文章

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

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]. 先排序,然后循环合并 1 /** 2 * Definition for an interval. 3 * struct Interval { 4 * int start; 5 * int

60. Insert Interval &amp;&amp; Merge Intervals

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], i

【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 排序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 [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].

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)