LeetCode刷题(26)
来源:互联网 发布:淘宝运营工资提成方案 编辑:程序博客网 时间:2024/06/14 20:26
Insert interval
不想复用上一题的代码,那么就好好搞清楚位置关系吧!对于给定节点node和带插入节点new:
只有当node.end > new.start时才考虑插入问题,那么此时考虑
new.start
# Definition for an interval.# class Interval(object):# def __init__(self, s=0, e=0):# self.start = s# self.end = eclass Solution(object): def insert(self, intervals, newInterval): """ :type intervals: List[Interval] :type newInterval: Interval :rtype: List[Interval] """ if not intervals : return [newInterval] elif newInterval.end < intervals[0].start: return [newInterval] + intervals elif newInterval.start > intervals[-1].end: return intervals + [newInterval] res = [] flag = 0 for node in intervals: if node.end < newInterval.start or flag == 1: res += [node] elif node.start > newInterval.start: if newInterval.end < node.start: res += [newInterval, node] flag = 1 elif newInterval.end <= node.end: newInterval.end = node.end res += [newInterval] flag = 1 else: continue else : if newInterval.end <= node.end: res += [node] flag = 1 else: newInterval.start = node.start continue if flag == 0: res += [newInterval] return res
阅读全文
0 0
- LeetCode刷题(26)
- 【leetcode】leetcode 刷题 笔记 (不定期更新)
- leetcode刷题(Python)
- LeetCode刷题(1)
- LeetCode刷题(2)
- LeetCode刷题(3)
- LeetCode刷题(废弃)
- LeetCode刷题(FizzBuzz)
- Leetcode刷题(1)
- Leetcode刷题(2)
- Leetcode刷题(3)
- Leetcode刷题(4)
- Leetcode刷题(5)
- Leetcode刷题(6)
- Leetcode刷题(7)
- Leetcode刷题(13)
- Leetcode刷题(17)
- LeetCode刷题(18)
- STM32采用普通的IO口来测量PWM的频率
- C语言学习内容总结2017/10/28
- spark中配置RedisClient的模板代码
- mysql 基本操作快速复习(三)
- GDB基本使用
- LeetCode刷题(26)
- ubuntu电源管理
- 《统计学习方法》笔记之---决策树
- 用Java打开记事本
- UCGUI学习之路(3)增加其它字体
- 二叉树
- 190. Reverse Bits
- Java 多态
- spring mvc 异常统一处理方式