LeetCode OJ 56. 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].
解答
这题首先根据起始点对Interval进行排序,然后从头开始遍历Interval,对遍历到的每一个Interval(用current表示),将其终止点作为界限,检查其后的Interval的起始点是否小于或等于界限,如果小于或等于界限,则比较该Interval的终止点和界限来决定是否对界限进行更新,并继续检查后面的Interval,否则就将current的起始点和界限作为一个新的Interval记录下来,并继续遍历(当然已经检查过并进行合并操作的Interval就不需要遍历了)。
下面是AC的代码:
/**
* 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:
static bool compare(struct Interval a, struct Interval b){
return a.start < b.start;
}
vector<Interval> merge(vector<Interval>& intervals) {
vector<Interval> ans;
sort(intervals.begin(), intervals.end(), compare);
vector<Interval>::iterator i, j;
for(i = intervals.begin(); i != intervals.end(); i++){
int temp = i->end;
for(j = i + 1; j != intervals.end(); j++){
if(j->start <= temp){
temp = max(temp, j->end);
}
else{
break;
}
}
j--;
ans.push_back(Interval(i->start, temp));
i = j;
}
return ans;
}
};
111
LeetCode OJ 56. Merge Intervals的更多相关文章
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- 【LeetCode】56. Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【一天一道LeetCode】#56. Merge Intervals
一天一道LeetCode系列 (一)题目 Given a collection of intervals, merge all overlapping intervals. For example, ...
- 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 题解 56. Merge Intervals
题目大意:给出一组区间,合并他们. 首先是排序,首先看start,start小的在前面.start相同的话,end小的在前面. 排序以后,要合并了. 我自己的笨方法,说实在的问题真的很多.提交了好几次 ...
- [leetcode sort]56. Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode OJ:Merge Intervals(合并区间)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8, ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- 56. Merge Intervals - LeetCode
Question 56. Merge Intervals Solution 题目大意: 一个坐标轴,给你n个范围,把重叠的范围合并,返回合并后的坐标对 思路: 先排序,再遍历判断下一个开始是否在上一个 ...
随机推荐
- T-SQL 事务
use StudentManager go declare @errorSum int --定义变量,用于累计事务执行过程中的错误 --初始化为0,即无错误 begin transaction beg ...
- T-SQL 简单子查询
1.使用变量的方式实现的查询 use StudentManageDB go declare @StuId int --查询张永利学号 select @StuId=StudentId from Stud ...
- 00012 - ps命令详解
使用权限:所有使用者使用方式:ps [options] [--help]说明:显示瞬间行程 (process) 的动态参数:ps的参数非常多, 在此仅列出几个常用的参数并大略介绍含义-A 列出所 ...
- 00006 - Linux中使用export命令设置环境变量
功能说明:设置或显示环境变量. #################################################################################### ...
- JavaScript、CSS样式收集
JS集: //给from一个名字然后在JavaScript的地方就可以用form的名字来调用form表单里input元素的value属性可以得到值 var val=form_name.input_na ...
- 基于pyQt5开发的股价显示器(原创)
#/usr/bin/env python # -*- coding: utf-8 -*- ''' @author="livermorium116" 为了绕开公司内网而开发的 股票实 ...
- ECharts动态加载堆叠柱状图的实例
一.引入echarts.js文件(下载页:http://echarts.baidu.com/download.html) 二.HTML代码: <div style="width: 10 ...
- linux:安装mysql管理工具phpmyadmin
1.下载phpmyadmin,下载地址:http://www.phpmyadmin.net/ 2.解压到/var/www/html/phpmyadmin/ 3.修改配置 cd /var/www/htm ...
- sql zhuan ORACLE
DbType OracleType SqlDatabase OracleDatabase SqlParam.cs SqlServerHelper.cs InsertByHashtableRetur ...
- Robot Operating System (ROS)学习笔记3---键盘控制
搭建环境:XMWare Ubuntu14.04 ROS(indigo) 转载自古月居 转载连接:http://www.guyuehome.com/253 一.创建控制包 catkin_creat ...