leetcode — merge-intervals
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/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].
*
*
*/
public class MergeIntevals {
/**
*
* 合并重叠的数组
*
* 先对数组按照左边界排序,然后依次合并相邻的数组
*
* @param list
* @return
*/
public List<IntervalArray> merge (List<IntervalArray> list) {
if (list.size() <= 1) {
return list;
}
Collections.sort(list);
List<IntervalArray> result = new ArrayList<IntervalArray>();
for (int i = 0; i < list.size(); i++) {
if (i > 0 && list.get(i - 1).end >= list.get(i).start) {
list.get(i-1).end = Math.max(list.get(i).end, list.get(i - 1).end);
} else {
result.add(list.get(i));
}
}
return result;
}
private static class IntervalArray implements Comparable<IntervalArray>{
int start;
int end;
public IntervalArray(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public int compareTo(IntervalArray o) {
return this.start - o.start;
}
@Override
public String toString() {
return "[" + start + ","+ end +']';
}
}
public static void main(String[] args) {
MergeIntevals mergeIntevals = new MergeIntevals();
List<IntervalArray> list = new ArrayList<IntervalArray>();
list.add(new IntervalArray(2, 6));
list.add(new IntervalArray(8, 10));
list.add(new IntervalArray(15, 18));
list.add(new IntervalArray(1, 3));
List<IntervalArray> result = mergeIntevals.merge(list);
System.out.println(Arrays.toString(result.toArray(new IntervalArray[result.size()])));
}
}
leetcode — merge-intervals的更多相关文章
- LeetCode: Merge Intervals 解题报告
Merge IntervalsGiven a collection of intervals, merge all overlapping intervals. For example,Given [ ...
- [LeetCode] Merge Intervals 排序sort
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- [LeetCode] Merge Intervals 合并区间
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
- [leetcode]Merge Intervals @ Python
原题地址:https://oj.leetcode.com/problems/merge-intervals/ 题意: Given a collection of intervals, merge al ...
- Leetcode Merge Intervals
Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,1 ...
- LeetCode() Merge Intervals 还是有问题,留待,脑袋疼。
感觉有一点进步了,但是思路还是不够犀利. /** * Definition for an interval. * struct Interval { * int start; * int end; * ...
- 56[LeetCode] .Merge Intervals
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
- 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- [Leetcode][Python]56: Merge Intervals
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 56: Merge Intervalshttps://oj.leetcode. ...
- Merge Intervals - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Merge Intervals - LeetCode 注意点 区间是无序的 每个区间start一定小于end 解法 解法一:首先以start的值从小到大来 ...
随机推荐
- sequence测试中的使用
1. create sequence : create sequence TEST_SEQUENCE minvalue 1 maxvalue 1000000000 start with 1 incre ...
- git使用之后悔药
1.工作区的代码想撤销 背景:有时候编写了一大段代码之后,想要撤销更改(执行add操作之前), 命令:git checkout -- <file路径> 使用git checkout -- ...
- scrapy的基础概念和流程
1. 什么是scrapy Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,我们只需要实现少量的代码,就能够快速的抓取. Scrapy 使用了Twisted['twɪstɪd]异步网 ...
- redux之applyMiddleware
redux之所以伟大就在于中间件了,中间件为redux提供了无限可能.redux中中间件是一个不太容易理解的概念,因为涉及到compose.hoc等函数式的概念,看源代码总是懵懵的感觉.今天我们就来详 ...
- android-audioRecord
android 录音功能 录音的大致流程,流程图可以在文件下载:mediarecord.vsdx 切换设备.谁去更新播放流,自动选择新的设备?流程?
- Timer of STM32
下面是STM32得定时器程序,分两个文件Timer.c和Timer.h /*************************************************************** ...
- WordPress数据结构分析
WordPress仅仅用了10 个表:wp_comments, wp_links, wp_options, wp_postmeta, wp_posts, wp_term_relationships, ...
- Python之旅Day7 面向对象&异常处理
########################################面向对象初识######################################### 面向对象简介 面向对象编 ...
- 11g直接路径读、相关参数、10949事件介绍
转载自刘向兵老师:http://www.askmaclean.com/archives/11g-direct-path-read-10949-_small_table_threshold-_seria ...
- [深入学习Web安全](11)之XSS玩法
[深入学习Web安全](11)之XSS玩法 本文转自:i春秋社区 前言这篇paper,我们将学习如何优雅的调戏XSS.我们会教大家一些不常用的,但很实用的XSS姿势.我们在正式进入主题之前,先来说一下 ...