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


对若干个区间进行合并,使合并后的区间没有重叠

先对区间按照左边界排序,然后顺序扫描,合并重叠的区间即可。              本文地址

代码1在原区间数组上操作,不使用额外的空间,但是需要删除多余的区间,这样会移动数组元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
  * 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 {
private :
     static  bool  comp(Interval a, Interval b)
     {
         return  a.start < b.start;
     }
public :
     vector<Interval> merge(vector<Interval> &intervals) {
         if (intervals.empty()) return  intervals;
         sort(intervals.begin(), intervals.end(), comp);
         vector<Interval>::iterator it1 = intervals.begin(), it2 = it1 + 1;
         while (it1 != intervals.end() && it2 != intervals.end())
         {
             if (it2->start <= it1->end)
             {
                 if (it1->end < it2->end)it1->end = it2->end;
                 it2++;
             }
             else
             {
                 //[it1+1, it2)范围内的区间可以从原数组删除
                 it1 = intervals.erase(it1+1, it2);
                 it2 = it1 + 1;
             }
         }
         if (it1 != intervals.end())
             intervals.erase(it1 + 1, it2);
         return  intervals;
     }
};

 

代码2用新数组来存储合并后的区间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
  * 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 {
private :
     static  bool  comp(Interval a, Interval b)
     {
         return  a.start < b.start;
     }
public :
     vector<Interval> merge(vector<Interval> &intervals) {
         if (intervals.empty()) return  intervals;
         sort(intervals.begin(), intervals.end(), comp);
         vector<Interval> res;
         res.push_back(intervals[0]);
         for ( int  i = 1; i < intervals.size(); i++)
         {
             Interval &p = res.back();
             if (intervals[i].start > p.end)res.push_back(intervals[i]);
             else  if (intervals[i].end > p.end)p.end = intervals[i].end;
         }
         return  res;
     }
};





本文转自tenos博客园博客,原文链接:http://www.cnblogs.com/TenosDoIt/p/3714681.html,如需转载请自行联系原作者

目录
相关文章
|
存储
LeetCode 352. Data Stream as Disjoint Intervals
给定一个非负整数的数据流输入 a1,a2,…,an,…,将到目前为止看到的数字总结为不相交的区间列表。
91 0
LeetCode 352. Data Stream as Disjoint Intervals
LeetCode 88. Merge Sorted Array
题意是给定了两个排好序的数组,让把这两个数组合并,不要使用额外的空间,把第二个数组放到第一个数组之中.
79 0
LeetCode 88. Merge Sorted Array
|
算法 测试技术
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 88. 合并两个有序数组 Merge Sorted Array
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
LeetCode 21. 合并两个有序链表 Merge Two Sorted Lists
|
人工智能 搜索推荐
LeetCode 56. Merge Intervals
给定间隔的集合,合并所有重叠的间隔。
85 0
Leetcode-Easy21. Merge Two Sorted Lists
Leetcode-Easy21. Merge Two Sorted Lists
102 0
Leetcode-Easy21. Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
LeetCode之Merge Two Sorted Lists
102 0
|
Java Python
LeetCode 21:合并两个有序链表 Merge Two Sorted Lists
将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解题思路: 迭代和递归都能解题。
984 0
|
Java
[LeetCode]Merge Sorted Array 合并排序数组
链接:https://leetcode.com/problems/merge-sorted-array/description/难度:Easy题目:88.
1013 0