leetcode56
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的更多相关文章
- [array] leetcode-56. Merge Intervals - Medium
leetcode-56. Merge Intervals - Medium descrition Given a collection of intervals, merge all overlapp ...
- leetcode56. Merge Intervals
题目要求: Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6 ...
- [Swift]LeetCode56. 合并区间 | Merge Intervals
Given a collection of intervals, merge all overlapping intervals. Example 1: Input: [[1,3],[2,6],[8, ...
- leetcode56:Merge Intervals
大都是自定义了 Interval的比较方法. 突发奇想 int [] arr=new int[intervals.Count*2]; for(int i=0;i<intervals.Count; ...
- leetcode56:合并区间
给出一个区间的集合,请合并所有重叠的区间.(解题思想来源于:https://blog.csdn.net/qq_34364995/article/details/80788049 ) 示例 1: 输入: ...
- LeetCode56:Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- Leetcode56. Merge Intervals合并区间
给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18]] 解释: 区间 [1,3] ...
- leetcode56之合并区间
题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例: 输入: [[1,3],[2,6],[8,10],[15,18]]输出: [[1,6],[8,10],[15,18]]解释: 区间 [1, ...
- leetcode56:restore-ip-addresses
题目描述 现在有一个只包含数字的字符串,将该字符串重新存储成IP地址的形式,返回所有可能的情况. 例如: 给出的字符串为"25525511135", 返回["255.25 ...
随机推荐
- selenium+grid做分布式测试
一.grid介绍 1.本文用的是selenium-server-standalone-3.8.1.jar 2.Firefox用的55版本和对应的驱动 二.grid使用流程说明比如有个A机器,作用是hu ...
- WPF Blend Grid 布局
这几天都在用blend拖拽界面.我想要的效果是 放大后出现的效果是 但实际出来的效果是放大以后能看到所有的控件,缩小以后窗体就把控件个遮住了.怎么办? 在WPF中提供了9种布局方式,具体Grid,Ca ...
- httpd does not appear to be running and proxying cobbler, or SELinux is in the way.
当我们执行cobbler check时,出现这种错误:httpd does not appear to be running and proxying cobbler, or SELinux is i ...
- Keil生成汇编文件、bin文件
// 生成汇编文件:$K\ARM\ARMCC\bin\fromelf.exe --text -a -c --output=@L_asm.txt "!L" // 生成bin文件:$K ...
- 自定义textview
#import <UIKit/UIKit.h> @class FSTextView; typedef void(^FSTextViewHandler)(FSTextView *textVi ...
- go语言入门(Hello World)
package main import "fmt" func main(){ fmt.Println("Hello world") }
- 【java多线程】队列系统之DelayQueue源码
一.延迟队列 延迟队列,底层依赖了优先级队列PriorityBlockingQueue 二.延迟队列案例 (1)延迟队列的任务 public class DelayTask implements De ...
- python_字符编码&格式化
电脑最小储存单位是bit(位),8bit为一个Byte(字节), 8bit=1Byte 1024Byte=1KB 1024KB=1MB 1024MB=1GB 1024GB=1TB 编码的故事: 计算机 ...
- 什么是FPGA的HP,HR I/O
什么是FPGA的HP,HR I/O HP接口为高速接口,用于存储器或者芯片与芯片之间的接口,HR可以接受很宽的电平标准.
- 用a标签实现submit提交按钮的效果
今天做了一个小项目练手,要求点击a标签后实现post提交的效果,看到这个的时候心理还是有一丝丝懵逼的,不过在朕的十秒钟思考之后有了头绪... 首先表单 <form action="te ...