博客
关于我
【leetcode】合并区间
阅读量:541 次
发布时间:2019-03-09

本文共 574 字,大约阅读时间需要 1 分钟。

题目:

给出一个区间的集合,请合并所有重叠的区间。

示例 1:

输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入: [[1,4],[4,5]]输出: [[1,5]]解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

 

思路:

先将数组按照行的第第一个元素升序排列。

比较相邻的数组a[1,4]和b[2,5],当b[0]<=a[1]时,两个数组表示的范围存在重合,数组的左值一定是a[0](更小的值做范围起点),而右值则为a[1] b[1]二者中更大的值。

java代码:

class Solution {    public int[][] merge(int[][] intervals) {        List
res = new ArrayList<>(); Arrays.sort(intervals, (a,b)->a[0]-b[0]); int i=0; while (i

 

由于水平有限,博客中难免会有一些错误,有纰漏之处恳请各位大佬不吝赐教!

推荐阅读:

 

 

 

 

  

 

及时更新最新文章和学习资料,一起来学习:

转载地址:http://rxhiz.baihongyu.com/

你可能感兴趣的文章
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>