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还没有发布,不得不 ...
随机推荐
- Hibernate双向一对多、双向多对多关联关系中的映射文件怎么写
这里以一对多关联关系为例.以Country类为一端,Competition类为多端. 一个国家可以有多个赛事,但是一个赛事只能属于一个国家. Country类 public class Country ...
- 9、SpringBoot-CRUD国际化
1).编写国际化配置文件: 2).使用ResourceBundleMessageSource管理国际化资源文件 3).在页面使用fmt:message取出国际化内容 步骤: 1).编写国际化配置文件, ...
- JavaScript:回调模式(Callback Pattern) (转载)
JavaScript:回调模式(Callback Pattern) 函数就是对象,所以他们可以作为一个参数传递给其它函数: 当你将introduceBugs()作为一个参数传递给writeCode() ...
- 【题解】洛谷P2827 [NOIP2016TG] 蚯蚓(优先队列)
题目来源:洛谷P2827 思路 阅读理解题 一开始以为是裸的优先队列而已 但是发现维护一个切开并且其他的要分别加上一个值很不方便 而且如果直接用优先队列会TLE3到4个点 自测85分 所以我们需要发现 ...
- NLP语言模型
语言模型: I. 基本思想 区别于其他大多数检索模型从查询到文档(即给定用户查询,如何找出相关的文档), 语言模型由文档到查询,即为每个文档建立不同的语言模型,判断由文档生成用户查 询的可能性有多大, ...
- 四、MapReduce 基础
是一个并行计算框架(计算的数据源比较广泛-HDFS.RDBMS.NoSQL),Hadoop的 MR模块充分利用了HDFS中所有数据节点(datanode)所在机器的内存.CUP以及少量磁盘完成对大数据 ...
- MyBatis-Plus工具快速入门使用
MyBatis-plus有什么特色 1.代码生成 2.条件构造器 对我而言,主要的目的是使用它强大的条件构建器. 快速使用步骤: 1.添加pom文件依赖 <dependency> < ...
- Paths with -a does not make sense.
最近开始使用为windows的系统,进行git操作的时候出现了一个小问题. 使用命令: E:\IdeaProjects\mmall>git commit -am 'first commit in ...
- 学习笔记 - Manacher算法
Manacher算法 - 学习笔记 是从最近Codeforces的一场比赛了解到这个算法的~ 非常新奇,毕竟是第一次听说 \(O(n)\) 的回文串算法 我在 vjudge 上开了一个[练习],有兴趣 ...
- parsing XML document from class path resource [applicationtext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationtext.xml] cannot be opened because it does not e
控制台异常: parsing XML document from class path resource [applicationtext.xml]; nested exception is java ...