【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 end;
 6  *     Interval() : start(0), end(0) {}
 7  *     Interval(int s, int e) : start(s), end(e) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<Interval> merge(vector<Interval> &intervals) {
13         vector<Interval> result;
14         if(intervals.empty()) return result;
15
16         sort(intervals.begin(),intervals.end(),cmp);
17
18         int start=intervals[0].start;
19         int end=intervals[0].end;
20
21         for(int i=1;i<intervals.size();i++)
22         {
23             if(intervals[i].start<=end)
24             {
25                 //注意[1,4],[2,3]的情况
26                 end=end<intervals[i].end?intervals[i].end:end;
27             }
28             else
29             {
30                 result.push_back(Interval(start,end));
31                 start=intervals[i].start;
32                 end=intervals[i].end;
33             }
34         }
35
36         result.push_back(Interval(start,end));
37         return result;
38     }
39
40     static int cmp(const Interval &a,const Interval &b)
41     {
42         if(a.start<b.start) return 1;
43         else if(a.start>b.start) return 0;
44         else return a.end<b.end;
45     }
46 };
时间: 2024-10-14 04:18:35

【leetcode】Merge Intervals的相关文章

【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]. [解析] 题意:有很多个区间,把有重叠的区间合并. 思路:先排序,然后检查相邻两个区间,看前一个区间的结尾是否大于后一个区间的开始,注意前一个区间包含后一个区间的情况. 用Java自带的sort()方法,只

【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】 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]. 解题思路: 根据start对区间进行排序,然后依次遍历进行区间合并. /** * Definition for an interval. * struct Interval { * int

【leetcode】Merge Intervals(hard)

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]. 思路:开始想用线段树,后来想想这个不是动态变化的没必要. 按区间的第一个值从小到大排序,然后跳过后面被覆盖的区间来找. sort折腾了好久,开始搞不定只好自己写了一个归并排序.现在代码里的sort是可用的. class

【LeetCode】Merge k Sorted Lists 解题报告

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并几个有序链表为一个,分析算法复杂度. [分治] 直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合并为newlist3,newlist3再和list4合并为newlist4--依次类推:2)list1和list2合并为li

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

【LeetCode】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.合并过程中,一个链表已经走到了末尾,即移动到了空指针,但另一个链表还

【leetcode】Merge Sorted Array

Merge Sorted Array 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 initialize

【leetcode】Merge Two Sorted Lists(easy)

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. 思路:使用伪头部 class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode fakehead(0)