public class Solution
{
public IList<Interval> Merge(IList<Interval> intervals)
{
var len = intervals.Count;
if (len == )
{
return intervals;
}
var list = intervals.OrderBy(x => x.start).ToList();
int i = ;
while (i < list.Count)
{
int j = i;
while (i < list.Count - && list[j].end >= list[i + ].start)
{
//可以融合
//前项修改,后项删除
list[j].start = Math.Min(list[j].start, list[i + ].start);
list[j].end = Math.Max(list[j].end, list[i + ].end);
list.RemoveAt(i + );
}
i++;
}
return list;
}
}

提供一个python版本的实现:

 class Solution:
def merge(self, intervals: 'List[Interval]') -> 'List[Interval]':
intervals = sorted(intervals,key=lambda x:[x[],-x[]])
i =
while i < len(intervals) - :
curbegin = intervals[i][]
curend = intervals[i][] nextbegin = intervals[i+][]
nextend = intervals[i+][]
if curend >= nextbegin:
intervals.pop(i)
intervals[i]=[min(curbegin,nextbegin),max(curend,nextend)]
else:
i +=
return intervals

leetcode56的更多相关文章

  1. [array] leetcode-56. Merge Intervals - Medium

    leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...

  2. leetcode56. Merge Intervals

    题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...

  3. [Swift]LeetCode56. 合并区间 | Merge Intervals

    Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...

  4. leetcode56:Merge Intervals

    大都是自定义了 Interval的比较方法. 突发奇想 int [] arr=new int[intervals.Count*2]; for(int i=0;i<intervals.Count; ...

  5. leetcode56:合并区间

    给出一个区间的集合,请合并所有重叠的区间.(解题思想来源于:https://blog.csdn.net/qq_34364995/article/details/80788049 ) 示例 1: 输入: ...

  6. LeetCode56:Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  7. Leetcode56. Merge Intervals合并区间

    给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...

  8. leetcode56之合并区间

    题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1, ...

  9. leetcode56:restore-ip-addresses

    题目描述 现在有一个只包含数字的字符串,将该字符串重新存储成IP地址的形式,返回所有可能的情况. 例如: 给出的字符串为"25525511135", 返回["255.25 ...

随机推荐

  1. vue-cli 安装过程出现错误

    如果是这样得错误,那是你在安装sass得问题,需要安装python2,安装好就行了

  2. CentOS7 cannot find a valid baseurl for repo base

    找到文件夹: cd /etc/sysconfig/network-scripts/ 然后找ifcfg 开头的文件,挨个打开 里面有下面那些代码前三行的就是,添加后面DNS两行,保持并退出,然后继续执行 ...

  3. 【leetcode】453. Minimum Moves to Equal Array Elements

    problem 453. Minimum Moves to Equal Array Elements 相当于把不等于最小值的数字都减到最小值所需要次数的累加和. solution1: class So ...

  4. 一个页面从输入url到加载完成的过程都发生了什么,请详细说明

    1.首先,在浏览器地址栏中输入url 2.浏览器先查看浏览器缓存-系统缓存-路由器缓存,如果缓存中有,会直接在屏幕中显示页面内容.若没有,则跳到第三步操作 3.在发送http请求前,需要域名解析(DN ...

  5. SQLI DUMB SERIES-11

    (1)检测构造方式 由此看出输入的用户名以及密码都被一对单引号所包含. 方法一: (2) 模拟真实环境,以用户的身份登录. (3)用burp抓包.开启抓包,输入用户名和密码,会自动跳到这个页面,右键, ...

  6. 14.python-CS编程

    一.客户端/服务器架构1.C/S架构:(1)硬件C/S架构(打印机)(2)软件C/S架构(web服务)2.生活中的C/S架构:饭店是S端,所有食客是C端3.C/S架构与socket的关系:socke就 ...

  7. 工作中 sql 整理(一)

    这篇文章记录关于SQL的内容,有些凌乱,是工作中点滴的积累,只能按照时间顺序,逐次记录. 一.update 关联更新 1.需求 Table A   TableB A表中的主键和B表中的主键相关联,关联 ...

  8. Getting Visual Studio version of a Solution file

    VS 6.0 -> 6.0 VS 2002 -> 7.0 VS 2003 -> 8.0 VS 2005 -> 9.0 VS 2008 -> 10.0 VS 2010 -& ...

  9. 让Mustache支持简单的IF语句

    转载:https://blog.csdn.net/iteye_16732/article/details/82070065 Mustache是一种Logic-less templates.不支持if这 ...

  10. webpack 中,importloaders 配置项的含义

    importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...