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)的更多相关文章

  1. 【leetcode】Merge Intervals(hard)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  2. LeetCode 56. Merge Intervals (合并区间)

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  3. [LeetCode] 56. Merge Intervals(vector sort)

    /** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0 ...

  4. leetcode 56. Merge Intervals 、57. Insert Interval

    56. Merge Intervals是一个无序的,需要将整体合并:57. Insert Interval是一个本身有序的且已经合并好的,需要将新的插入进这个已经合并好的然后合并成新的. 56. Me ...

  5. LeetCode: 61. Rotate List(Medium)

    1. 原题链接 https://leetcode.com/problems/rotate-list/description/ 2. 题目要求 给出一个链表的第一个结点head和正整数k,然后将从右侧开 ...

  6. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  7. LeetCode:11. ContainerWithWater(Medium)

    原题链接:https://leetcode.com/problems/container-with-most-water/description/ 题目要求:给定n个非负整数a1,a2,...,an  ...

  8. [LeetCode] 56 - Merge Intervals 合并区间

    Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...

  9. Leetcode#56 Merge Intervals

    原题地址 排序+合并,没啥好说的 第一次尝试C++的lambda表达式,有种写js的感觉,很神奇 c11就支持了lambda表达式,仔细想想,我学C++大概就是在09~10年,c11还没有发布,不得不 ...

随机推荐

  1. Django CreateView 简单使用

    django.views.generic中的CreateView类,是基于View的子类.CreateView可以简单快速的创建表对象. 下面记录小作代码. # polls/views.py from ...

  2. Linear Search

    Search I You are given a sequence of n integers S and a sequence of different q integers T. Write a ...

  3. mybaitis动态sql利用bind标签代替%拼接完成模糊查询

    Oracle中使用bind的写法 <select id="selectUser" resultType="user" parameterType=&quo ...

  4. Servlet是线程安全的吗

    Servlet默认是单例模式,在web容器中只创建一个实例,所以多个线程同时访问Servlet是不安全的. 解决此类问题的办法是: 只要Servlet实现 SingleThreadModel 接口

  5. 使用cmd命令创建vue(ivieiw)项目

    条件,安装好nodejs 第一步:先使用 vue create 命令创建一个项目,等待创建完成. 1.切换目录 2.创建项目  vue create [项目名称] 第二步:切换到项目中. 第三步:使用 ...

  6. $.ajax方法success方法窗口弹不出

  7. NodeJs——入门

    关于NPM: npm 是 nodejs 的包管理和分发工具.它可以让 javascript 开发者能够更加轻松的共享代码和共用代码片段,并且通过 npm 管理你分享的代码也很方便快捷和简单. 一 No ...

  8. 【oracle笔记3】多表查询

    *多表查询 分类:1.合并结果集 2.连接查询 3.子查询 *合并结果集:要求被合并的表中,列的类型和列数相同. *UNION,去除重复行.完全相同的行会被去除 *UNION ALL:不去除重复行. ...

  9. Swift_类和结构体

    Swift_类和结构体 点击查看源码 struct Resolution { var width = 0 var height = 0 } class VideoMode { var resoluti ...

  10. Linux CentOs下安装lamp

    以home目录为例,进入/home cd /home 是否安装gcc与gcc-c++,没有则安装 yum -y install gcc gcc-c++ 一.安装Apache 下载httpd wget ...