zoukankan      html  css  js  c++  java
  • 边工作边刷题:70天一遍leetcode: day 29-1

    Insert Interval

    虽然现在看算法很简单,但是实现比较tricky,另外算法背后的逻辑比较不容易想。
    记忆要点:

    1. 因为interval list已经是排好序的了,核心就是顺序比较当前的interval和newInterval。主要分成两种情况:newInterval可以直接插入或者不确定。对于前者,就是newInterval.end<interval.start
    2. 这题不是in-place的,因为没法确定要删除的interval的个数
    3. 过程就是问下一个要push的interval是哪个?newInterval or next in intervals or overlapping
    # Definition for an interval.
    # class Interval(object):
    #     def __init__(self, s=0, e=0):
    #         self.start = s
    #         self.end = e
    
    class Solution(object):
        def insert(self, intervals, newInterval):
            """
            :type intervals: List[Interval]
            :type newInterval: Interval
            :rtype: List[Interval]
            """
            i=0
            res = []
            while i<len(intervals):
                if intervals[i].start>newInterval.end:
                    res.append(newInterval)
                    res+=intervals[i:]
                    return res
                elif newInterval.start>intervals[i].end:
                    res.append(intervals[i])
                    i+=1
                else:
                    newInterval.start=min(newInterval.start, intervals[i].start)
                    newInterval.end=max(newInterval.end, intervals[i].end)
                    i+=1
            res.append(newInterval)
            return res
                    
    
  • 相关阅读:
    python播放音频文件
    安装pyaudio
    给 python工程 打包并上传 PyPI (The Python Package Index)
    python怎么import自己写的包
    pip源使用国内镜像
    Git和GitHub
    nginx的日志轮转
    ab接口压力测试工具
    nginx 性能优化
    https协议
  • 原文地址:https://www.cnblogs.com/absolute/p/5678094.html
Copyright © 2011-2022 走看看