【leetcode】 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]
.
解题思路: 根据start对区间进行排序,然后依次遍历进行区间合并。
/**
* Definition for an interval.
* struct Interval {
* int start;
* int end;
* Interval() : start(0), end(0) {}
* Interval(int s, int e) : start(s), end(e) {}
* };
*/
bool cmp(const Interval &a, const Interval &b){
return a.start < b.start;
}
class Solution {
public:
vector<Interval> merge(vector<Interval> &intervals) {
if(intervals.size() == )
return intervals;
sort(intervals.begin(), intervals.end(), cmp);
vector<Interval> result;
Interval mover = intervals[];
for(int i = ; i < intervals.size(); ++i){
if(mover.end < intervals[i].start){
result.push_back(mover);
mover = intervals[i];
}else{
mover.end = max(mover.end, intervals[i].end);
}
}
result.push_back(mover);
return result;
}
};
【leetcode】 Merge Intervals的更多相关文章
- 【LeetCode】Merge Intervals 题解 利用Comparator进行排序
题目链接Merge Intervals /** * Definition for an interval. * public class Interval { * int start; * int e ...
- 【leetcode】Merge Intervals
Merge Intervals Given a collection of intervals, merge all overlapping intervals. For example,Given ...
- 【leetcode】Merge Intervals(hard)
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【Leetcode】【Hard】Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- 【LeetCode】Merge Sorted Array(合并两个有序数组)
这道题是LeetCode里的第88道题. 题目描述: 给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: 初始化 nu ...
- 【leetcode】Merge Sorted Array
题目描述 Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assu ...
- 【leetcode】Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 【leetcode】Merge Two Sorted Lists(easy)
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...
随机推荐
- db2安装
官网下载: DB2 11.1 data server trial for Linux® on AMD64 and Intel® EM64T systems (x64)v11.1_linuxx64_se ...
- 12.9 Daily Scrum
在一些实现上,开发人员提出了意见,经过讨论后,我们决定取消“推荐餐厅”的功能,增加了“菜谱分类”的功能. 同时更新了相关人员的任务. Today's Task Tomorrow's Task 丁辛 ...
- IT行业创新的读后感
一.什么是创新 创新是以新思维.新发明和新描述为特征的一种概念化过程.它原意有三层含义,第一,更新:第二,创造新的东西:第三,改变.创新是人类特有的认识能力和实践能力,是人类主观能动性的高级表现形式, ...
- python中的文件读写(open()函数、with open('file_directory','r') as f:、read()函数等)
python中也有文件读写,通过调用内置的读写函数.可以完成文件的打开/关闭.读.写入.追加等功能. open()函数 open()函数为python中的打开文件函数,使用方式为: f = open( ...
- SpringMvc配置扫包之后,访问路径404问题解决
场景: 1. 配置了Spring和SpringMvc, Spring管理非Controller类的Bean, SpringMvc管理涉及的Controller类 2. web.xml已经配置了Spri ...
- shell之重定向
使用>和>>都表示向结果重定向到一个文件中,区别在于>是覆盖式的重定向,会先将内容先清空,然后再将结果输入,而>>是追加式的重定向,是将要输入的内容追加在在已存在的 ...
- CI框架--数据库Query_Builder中的方法
下面是DB_Query_Builder.php中,各个方法的声明: 选择字段(select) public function select($select = '*', $escape = NULL) ...
- K3CLOUD新增用户
1.在金蝶云之家对应的产品序列中新增用户 2.在CLOUD本地查询用户-同步注册用户后,云平台用户会同步至本地
- Spring AOP切点表达式用法总结
1. 简介 面向对象编程,也称为OOP(即Object Oriented Programming)最大的优点在于能够将业务模块进行封装,从而达到功能复用的目的.通过面向对象编程,不同的模 ...
- javaIO缓冲区
java中IO类分类. 图来自网络 缓冲区:应用程序在内存中开辟的一个空间.用来放置需要被写入或写出的数据. 使用缓冲区的 优点:使得应用程序操作磁盘(或者说是与磁盘的通信)的次数降低,提高应用程序的 ...