其他分享
首页 > 其他分享> > 去除重叠区间

去除重叠区间

作者:互联网

题目:去除区间中重叠的部分

intput1 :  [[1, 2], [1, 3], [2, 3], [3, 4]]
output1:  [[1, 2], [2, 3], [3, 4]]
input2:    [[1, 2], [1, 2], [1, 2]]
output2:  [[1, 2]]
input3:    [[1, 2], [2, 3]]
output3:  [[1, 2], [2, 3]]

思路:

将区间进行排序,从小到大,获取第一个区间的末尾值,往后开始遍历,区间的第一个值,

如果小于最小区间的末尾值,说明重叠了,就去掉。

代码实现:

#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File        :python.py
@Description :
@CreatTime   :2020/09/29 15:02:53
@Author      :Yunhgu
@Version     :1.0
'''
def eraseOverlapIntervals(args_doublelist):
    sorted_doublelist = sorted(args_doublelist)
    print(sorted_doublelist)
    curEnd = sorted_doublelist[0]
    for list_int in sorted_doublelist[1:]:
        if list_int[0]<curEnd[-1]:
            sorted_doublelist.remove(list_int)            
    return sorted_doublelist


if __name__ == "__main__":
    doubleList_int = [[1, 2],[2, 3], [3, 4],[1, 3]]
    removeOvrelap = eraseOverlapIntervals(doubleList_int)
    print(removeOvrelap)
    args = [ [1,2], [1,2], [1,2] ]
    print(eraseOverlapIntervals(args))
    args2 = [ [1,2], [2,3] ]
    print(eraseOverlapIntervals(args2))

 

标签:args,重叠,python,去除,sorted,区间,doublelist
来源: https://www.cnblogs.com/yunhgu/p/13852846.html