LeetCode: 56. Merge Intervals(Medium)
1. 原题链接
https://leetcode.com/problems/merge-intervals/description/
2. 题目要求
给定一个Interval对象集合,然后对重叠的区域进行合并。Interval定义如下

例如下图中,[1, 3] 和 [2, 6]是有重叠部分的,可以合并成[1, 6]

3. 解题思路
先取第一个interval对象的 start 和 end 的值 ,然后对这个集合进行遍历。比较当前遍历对象的start是否比前一个对象的end小,小的话则说明二者存在覆盖,然后对二者进行合并
4. 代码实现
import java.util.LinkedList;
import java.util.List; public class MergeIntervals56 {
public static void main(String[] args) {
Interval in1 = new Interval(1, 3);
Interval in2 = new Interval(2, 6);
Interval in3 = new Interval(8, 10);
Interval in4 = new Interval(15, 18);
List<Interval> ls = new LinkedList<Interval>();
ls.add(in1);
ls.add(in2);
ls.add(in3);
ls.add(in4);
for (Interval in : merge(ls))
System.out.println(in.start + " " + in.end);
} public static List<Interval> merge(List<Interval> intervals) {
if (intervals.size() <= 1)
return intervals;
List<Interval> res = new LinkedList<Interval>();
intervals.sort((i1, i2) -> Integer.compare(i1.start, i2.start));
int start = intervals.get(0).start;
int end = intervals.get(0).end;
for (Interval in : intervals) {
if (in.start <= end) {
end = Math.max(in.end, end);
} else {
res.add(new Interval(start, end));
start = in.start;
end = in.end;
}
}
res.add(new Interval(start, end));
return res;
}
} class Interval {
int start;
int end; Interval() {
start = 0;
end = 0;
} Interval(int s, int e) {
start = s;
end = e;
}
}
LeetCode: 56. Merge Intervals(Medium)的更多相关文章
- 【leetcode】Merge Intervals(hard)
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(vector sort)
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...
- leetcode 56. Merge Intervals 、57. Insert Interval
56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...
- LeetCode: 61. Rotate List(Medium)
1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- LeetCode:11. ContainerWithWater(Medium)
原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an ...
- [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
原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...
随机推荐
- Nginx卸载重装问题
1.删除nginx,-purge包括配置文件 sudo apt-get --purge remove nginx 2.移除全部不使用的软件包 sudo apt-get autoremove 3 ...
- phpstudy mysql 升级5.7.18
1.从官网下载MySQL http://dev.mysql.com/downloads/mysql/ 2.解压到想安装到的 例如 C:\phpStudy\MySQL 进入该目录,找到my-defua ...
- 剑指offer 14 调整数组顺序使奇数位于偶数前面
牛客网上的题目还有一个额外的要求,就是不改变数组原始的前后数据,这种可以用队列来存储,或者把前后比较变为相邻的元素比较. 这个题目,主要要考察扩展性,用func函数就实现了扩展性.只需要改func函数 ...
- 敏捷开发系列之旅 第五站(不一样的RUP统一软件开发过程)
概述 RUP,统一软件开发过程,是一个面向对象且基于网络的程序开发方法论.根据Rational的说法,RUP就好像一个在线的指导者,他可以为所有方面和层次的程序开发提供指导方针.模板以及事例支持. ...
- iOS 帧动画之翻转和旋转动画
记录两个比较简单的动画,一个是翻转的动画,一个是旋转的动画. 旋转动画: 1 [UIView animateWithDuration:3 animations:^{ if (formView) { f ...
- HDU 2079 选课时间(普通型 数量有限 母函数)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2079 选课时间(题目已修改,注意读题) Time Limit:1000MS Memory Li ...
- Visual Studio Installer打包安装项目VS2015
使用VS2015的Visual Studio Installer打包安装项目,虽然整体操作很简单,但还是有几个特殊的点需要记一下,故写下此博客方便以后查阅 第一步,创建安装项目 如下: 里面最左侧的框 ...
- IE下内容居中
ie8下调了很长时间的居中问题,加一个body {text-align:center;},居然解决了.. 参考解决答案:*html * {margin:0px; padding:0;} 然后在盒子里b ...
- 【读书笔记】The Swift Programming Language (Swift 4.0.3)
素材:Language Guide 初次接触 Swift,建议先看下 A Swift Tour,否则思维转换会很费力,容易卡死或钻牛角尖. 同样是每一章只总结3个自己认为最重要的点.这样挺好!强迫你去 ...
- 数据库函数(Left、Right)
MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...