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]
.
题目的意思是将相交得区间合并,典型的贪心算法
首先将区间先按照start进行排序,
然后保存先前区间的start和end
如果当前的start > 先前的end,说明当前的区间与之前的区间不想交,则将先前的区间放入结果中,同时更新start和end
如果当前的start < 先前的end,说明当前的区间与先前的区间相交,故比较当前的end与先前的end,如果当前的end大于先前的end,则更新先前的end为当前的end
bool cmp(const Interval& a, const Interval& b){
if(a.start!=b.start) return a.start < b.start;
else return a.end < b.end;
} class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> res;
if(intervals.size() == ) return res;
int start = intervals[].start, end = intervals[].end;
for(int i = ; i < intervals.size(); ++ i){
if(intervals[i].start > end){
res.push_back(Interval(start,end));
start = intervals[i].start, end = intervals[i].end;
}else{
end = max(end, intervals[i].end);
}
}
res.push_back(Interval(start,end));
return res;
}
};
Leetcode Merge Intervals的更多相关文章
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
随机推荐
- C#直接赋值和反射赋值(无GC)的性能比较
using System; using System.Reflection; using System.Diagnostics; using System.Runtime.InteropService ...
- Spring系列之bean的使用
一.Bean的定义 <bean id="userDao" class="com.dev.spring.simple.MemoryUserDao"/> ...
- js中join和split的用法
- (转)PostgreSQL 兼容Oracle - orafce
转自:http://blog.163.com/digoal@126/blog/static/1638770402015112144250486/ PostgreSQL是和Oracle最接近的企业数据库 ...
- java运行过程
一.安装环境 大家在开发Java的时候,首先回装一个java的开发环境,一个JDK(也包含了JRE),然后设置环境变量,这个过程我就不细说了,大家装完后有没有发现,在装完这个环境的同时在安装JRE,在 ...
- HDU 1003 maxsum
#include<iostream> #include<vector> using namespace std; typedef struct { int maxsum; in ...
- DataTable数据检索的性能分析(转寒江独钓)
我们知道在.NET平台上有很多种数据存储,检索解决方案-ADO.NET Entity Framework,ASP.NET Dynamic Data,XML, NHibernate,LINQ to SQ ...
- PHP中curl的CURLOPT_POSTFIELDS参数使用细节
CURL确实是一个不错的好工具,不仅在PHP中还是其他的操作系统中,都是一个非常好用的.但是如果你有些参数没有用好的话,那可能会得不到自己理想中的结果. 在通常情况下,我们使用 CURL 来提交 PO ...
- 第2月第5天 arc invocation getReturnValue
http://blog.csdn.net/zengconggen/article/details/38024625
- java.lang.UnsatisfiedLinkError: Couldn't load BaiduMapSDK 的解决方法
遇到找不到so的同学们可以先从以下几个方面来检查问题: 1.so的名字是不是被修改了?我们SDK的so名字是固定的,如果您自行对它进行了重命名操作,那肯定是没法找到so的.2.so放置位置不对.so需 ...