Merge Intervals

代码:

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4
 5 using namespace std;
 6
 7 struct Interval {
 8     int start;
 9     int end;
10     Interval() : start(0), end(0) {}
11     Interval(int s, int e) : start(s), end(e) {}
12 };
13
14 bool compare(Interval &a, Interval &b)
15 {
16     return a.start < b.start;
17 }
18
19 vector<Interval> merge(vector<Interval>& intervals)
20 {
21     vector<Interval> result;
22     sort(intervals.begin(), intervals.end(), compare);
23     for (int i = 0; i < intervals.size()-1; i++)
24     {
25         if ((i+1!=intervals.size()-1)&&intervals[i].end >= intervals[i + 1].start)
26         {
27             intervals[i].end = intervals[i + 1].end;
28         }
29         else
30         {
31             result.push_back(intervals[i]);
32         }
33     }
34     return result;
35 }
36
37
38 int main()
39 {
40     Interval I1(1,3);
41     Interval I2(2,6);
42     Interval I3(8, 10);
43     Interval I4(15,18);
44     vector<Interval> itervals;
45     itervals.push_back(I1);
46     itervals.push_back(I2);
47     itervals.push_back(I3);
48     itervals.push_back(I4);
49
50     vector<Interval> result = merge(itervals);
51     for (int i = 0; i < result.size(); i++)
52     {
53         cout << result[i].start << "    " << result[i].end << endl;
54     }
55 }
时间: 2024-12-12 13:44:19

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

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

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)

056 Merge Intervals

题目: 056 Merge Intervals 这道题和 057 基本相似, 想法更加直接, 对start 进行排序,然后扫描一次并跟新返回的答案 class Solution: # @param {Interval[]} intervals # @return {Interval[]} def merge(self, intervals): ans = [] for i in sorted(intervals, key=lambda i:i.start): if ans and ans[-1]