• 【leetcode】1272. Remove Interval


    题目如下:

    Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b] represents the set of real numbers x such that a <= x < b.

    We remove the intersections between any interval in intervals and the interval toBeRemoved.

    Return a sorted list of intervals after all such removals.

    Example 1:

    Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
    Output: [[0,1],[6,7]]
    

    Example 2:

    Input: intervals = [[0,5]], toBeRemoved = [2,3]
    Output: [[0,2],[3,5]]

    Constraints:

    • 1 <= intervals.length <= 10^4
    • -10^9 <= intervals[i][0] < intervals[i][1] <= 10^9

    解题思路:区间减法,判断两个区间的位置关系即可。

    代码如下:

    class Solution(object):
        def removeInterval(self, intervals, toBeRemoved):
            """
            :type intervals: List[List[int]]
            :type toBeRemoved: List[int]
            :rtype: List[List[int]]
            """
            res = []
            for start,end in intervals:
                if end <= toBeRemoved[0] or start > toBeRemoved[1]:
                    res.append([start,end])
                elif start == toBeRemoved[0] and end == toBeRemoved[1]:
                    continue
                elif start == toBeRemoved[0] and end < toBeRemoved[1]:
                    continue
                elif start == toBeRemoved[0] and end > toBeRemoved[1]:
                    res.append([toBeRemoved[1],end])
                elif start >= toBeRemoved[0] and end == toBeRemoved[1]:
                    continue
                elif start >= toBeRemoved[0] and end <= toBeRemoved[1]:
                    continue
                elif start >= toBeRemoved[0] and end > toBeRemoved[1]:
                    res.append([toBeRemoved[1],end])
                elif start < toBeRemoved[0] and end == toBeRemoved[1]:
                    res.append([start,toBeRemoved[0]])
                elif start < toBeRemoved[0] and end > toBeRemoved[1]:
                    res.append([start,toBeRemoved[0]])
                    res.append([toBeRemoved[1],end])
                elif start < toBeRemoved[0] and end < toBeRemoved[1]:
                    res.append([start, toBeRemoved[0]])
            return res
  • 相关阅读:
    使用Jmeter测试java请求
    如何高效开发jmeter自定义函数
    使用Fiddler进行抓包
    使用Jmeter导出导入接口自动化案例中的自定义变量
    使用Jmeter录制脚本并调试
    python练习——第3题
    python练习——第2题
    python练习——第1题
    python练习——第0题
    python机器学习——逻辑回归
  • 原文地址:https://www.cnblogs.com/seyjs/p/11967408.html
Copyright © 2020-2023  润新知