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还没有发布,不得不 ...
随机推荐
- phonegap的照相机 API
一. Camera Api 简单介绍 Camera 选择使用摄像头拍照,或从设备相册中获取一张照片.图片以 base64 编码的 字符串或图片 URI 形式返回. 方法: 1. camera.getP ...
- poj3259 Wormholes【Bellman-Ford或 SPFA判断是否有负环 】
题目链接:poj3259 Wormholes 题意:虫洞问题,有n个点,m条边为双向,还有w个虫洞(虫洞为单向,并且通过时间为倒流,即为负数),问你从任意某点走,能否穿越到之前. 贴个SPFA代码: ...
- Kali Linux重设root密码
许久不用的Kali,某天打开竟忘了密码! 网上的方法颇为简单,遂准备亲自试一下. #光标移动到第二行的“恢复模式”,按E进入[编辑模式] ...
- WEB测试—功能测试
1. 链接测试 1.1 测试点: 是否添加链接 链接页面是否存在 链接页面与需求是否一致:页面的正确性.打开方式 等 一般,该链接测试在集成测试阶段(页面均开发 ...
- SSH secure 连接centos7乱码
1.SSH secure 连接centos7乱码 修改文件 /etc/local.conf LANG="zh_CN.UTF-8"修改为LANG=”zh_CN.GB18030” 然 ...
- 基于jQuery+JSON的省市县 二级 三级 联动效果
省市区联动下拉效果在WEB中应用非常广泛,尤其在一些会员信息系统.电商网站最为常见.开发者一般使用Ajax实现无刷新下拉联动.本文将讲述,利用jQuery插件,通过读取JSON数据,实现无刷新动态下拉 ...
- LeetCode 中级 -二叉树的层次遍历(102)
题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如:给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...
- Handshake Lemma
- finite undirected gragh ∑deg(v) = 2|E| In a k-ary tree where every node has either 0 or k children ...
- c++11线程创建的三种方法
一.用一个初始函数创建一个线程 直接看代码:注意c++在运行一个可执行程序的时候(创建了一个进程),会自动的创建一个主线程,这个主线程和进程同生共死,主线程结束,进程也就结束了. #include & ...
- ubuntu查找端口和kill
查看 : netstat -anp | grep 8080 结束: kill -9 进程号