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.
 * struct Interval {
 *     int start;
 *     int end;
 *     Interval() : start(0), end(0) {}
 *     Interval(int s, int e) : start(s), end(e) {}
 * };
 */

//方法:先sort,然后再考虑情况合并。复杂度:O(nlogn)
class Solution {
public:
    struct compare {
        bool operator()( const Interval& a , const Interval& b) const {
            if( a.start==b.start)
                return a.end < b.end;
            else
                return a.start < b.start;
        }
    };

	vector<Interval> merge(vector<Interval> &intervals) {
	    if(intervals.size()==0)
	        return vector<Interval>();
	    sort(intervals.begin() , intervals.end() , compare());
	    vector<Interval> ans;
	    Interval temp;
	    temp = intervals[0];
	    for(int i=1; i<intervals.size(); i++)
	    {
	        if(intervals[i].start <= temp.end )
	            temp.end = max(intervals[i].end , temp.end);
	        else
            {
                ans.push_back(temp);
                temp = intervals[i];
            }
	    }
	    ans.push_back(temp);
	    return ans;
	}
};
时间: 2024-10-07 00:45:41

leetcode_56_Merge Intervals的相关文章

PKU1201 Intervals

Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

POJ1201 Intervals查分约束系统(最短路)

Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a program that: reads the number of intervals, their end points and integers c1, ..., cn from the standard input, computes the minimal size of a set Z of

ural 1346. Intervals of Monotonicity

1346. Intervals of Monotonicity Time limit: 1.0 secondMemory limit: 64 MB It’s well known that a domain of any continuous function may be divided into intervals where the function would increase monotonically or decrease monotonically. A number of in

poj 3225 Help with Intervals(线段树)

题目链接:poj 3225 Help with Intervals 题目大意:模拟集合操作,输出最终的集合. 解题思路:线段树. U l r:[l,r]区间置为1 I l r:[0,l),(r,maxn]置为0 D l r:[l,r]区间置为0 C l r:[0,l),(r,maxn]置为0,[l,r]区间取逆 S l r:[l,r]区间取逆. 然后基本水水的线段树,注意一下区间开和闭. #include <cstdio> #include <cstring> #include &

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的方法,开一个总区间跨度的数组.对于有区间覆盖的数

POJ1375 Intervals(直线与圆切线、线段合并)

题目链接: http://poj.org/problem?id=1375 题目描述: Intervals Description In the ceiling in the basement of a newly open developers building a light source has been installed. Unfortunately, the material used to cover the floor is very sensitive to light. It

(线段树)poj3225-Help with Intervals

LogLoader, Inc. is a company specialized in providing products for analyzing logs. While Ikki is working on graduation design, he is also engaged in an internship at LogLoader. Among his tasks, one is to write a module for manipulating time intervals

POJ No.3680 Intervals

2016-06-01 22:01:39 题目链接: POJ No.3680 Intervals 题目大意: 给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大 解法: 费用流 建模: 区间的端点之间按照副权流量1连接,而每个点之间需要再连0权流量无穷作为跳过用 注意的地方: 十万个点肯定是不行的,看我unique离散化大法 1 //Intervals (POJ No.3680) 2 //费用流 3 #include<stdio.h> 4 #include<algori

[LeetCode]题解(python):056-Merge Intervals

题目来源 https://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],[8,10],[15,18]. 题意分析 Input:a list of intervals Output:a list of intervals