设为首页 加入收藏

TOP

leetcode_56_Merge Intervals
2015-07-20 17:18:00 来源: 作者: 【 】 浏览:3
Tags: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
  
    merge(vector
   
     &intervals) { if(intervals.size()==0) return vector
    
     (); sort(intervals.begin() , intervals.end() , compare()); vector
     
       ans; Interval temp; temp = intervals[0]; for(int i=1; i
      
       

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇UVA1626 / ZOJ1463 Brackets sequ.. 下一篇POJ 2230 Watchcow (欧拉回路)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)