【一天一道LeetCode】#56. Merge Intervals
一天一道LeetCode系列
(一)题目
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、对vector里面的每个Interval结构体按照start的值升序排列
2、遍历整个intervals,如果有重复区域就合并,如果没有就存到ret里面
*/
/**
* 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 {
public:
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> ret;
if(intervals.empty()) return ret;
sort(intervals.begin(),intervals.end(),comparein);///按照start的值升序排列
Interval tmp = intervals[0];//初始化
for(int i = 1 ; i < intervals.size() ; i++)
{
if(intervals[i].start<=tmp.end) tmp.end = max(tmp.end,intervals[i].end);//更新end
else{
ret.push_back(tmp);//没有重复区域就压入ret
tmp = intervals[i];//更新tmp的值,继续遍历
}
}
ret.push_back(tmp);
return ret;
}
static bool comparein(const Interval& i1 , const Interval& i2)//比较函数
{
return i1.start<i2.start;
}
};
【一天一道LeetCode】#56. Merge Intervals的更多相关文章
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- LeetCode 56. Merge Intervals (合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode: 56. Merge Intervals(Medium)
1. 原题链接 https://leetcode.com/problems/merge-intervals/description/ 2. 题目要求 给定一个Interval对象集合,然后对重叠的区域 ...
- Leetcode#56 Merge Intervals
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
- [LeetCode] 56. Merge Intervals 解题思路
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] 56 - Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [leetcode]56. Merge Intervals归并区间
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- [LeetCode] 56. Merge Intervals(vector sort)
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...
- LeetCode 56. Merge Intervals 合并区间 (C++/Java)
题目: Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6] ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
随机推荐
- Android开发技巧——BaseAdapter的另一种优雅封装
RecyclerView虽然因其灵活性.高效性等特点而备受好评,但也不是一定得用它把ListView给替代掉.在某些场景中,ListView还是相对更适合的.比如数据量不大,不频繁更新,并且需要简单地 ...
- Mybatis 批量插入、批量更新
合理的使用批量插入.更新对性能优化有很大的作用,速度明显快了N倍. 要注意数据库连接串后面要新增:&allowMultiQueries=true,表示一个sql ...
- mysql和postgresql转义字符探究
总结 mysql依靠反斜杠\转义, postgresql 依靠单引号转义 mysql 客户端 mysql> create table usr (name varchar(), age integ ...
- Bootstrap3 栅格系统-媒体查询
在栅格系统中,我们在 Less 文件中使用以下媒体查询(media query)来创建关键的分界点阈值. /* 超小屏幕(手机,小于 768px) */ /* 没有任何媒体查询相关的代码,因为这在 B ...
- 【伯乐在线】Java线程面试题 Top 50
本文由 ImportNew - 李 广 翻译自 javarevisited.欢迎加入翻译小组.转载请见文末要求. 不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特 ...
- NSDictionary writeToFile:atomically: 时失败
一.现象:如下图,当NSDictionary调用 writeToFile:atomically: 时,如果容器里面包含的对象(即通过 objectForKey: 拿到的对象),为非property l ...
- Dynamics CRM2016 Web API之Retrieve Multiple
之前的博文只介绍了通过记录的primary key来查询单条记录或者单个属性值,本篇介绍多条记录的查询方法 var filter = "?$filter=name eq '123'" ...
- Ejb远程调用-jboss服务器调用服务器-Bean调用Bean
英文参考地址 https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+o ...
- [ExtJS5学习笔记]第三十二节 sencha extjs 5与struts2的ajax交互配置
本文地址:http://blog.csdn.net/sushengmiyan/article/details/43487751 本文作者:sushengmiyan ------------------ ...
- 关于MT8127中sdk的编译出错问题
今天在看MTK提供的SDK编译文档,按照步骤做,结果出错了,文档如下: 2- Building an SDK for MacOS and Linux ------------------------- ...