Leetcode 056 合并区间 python

题目:

给出一个区间的集合,请合并所有重叠的区间。

示例 1:

输入: [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].

示例 2:

输入: [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。

算法过程(伪代码):

遍历输入,把能融合的融合,不能融合的不管跳过。

算法证明:

当我们把原输入按照开始的第一项排序后,我们能保证以下2点:

1. 出现断层,则前一项的end一定小于后一项的start

2.更新答案时的最后一项的end永远是最大的。

既然最后一项区间的end永远最大,我们在融合的时候只需要考虑与新加入的区间与原最后一项的关系即可。

代码以及反思:


class Solution:
    def merge(self, intervals):
        intervals.sort(key=lambda x: x.start)

        merged = []
        for interval in intervals:
            # if the list of merged intervals is empty or if the current
            # interval does not overlap with the previous, simply append it.
            if not merged or merged[-1].end < interval.start: #如果当前merged为空或者当前项与最后一项连接不上,(连接的意思是说是否能融合成一个区间)我们直接添加
                merged.append(interval)
            else:
            # otherwise, there is overlap, so we merge the current and previous
            # intervals.
                merged[-1].end = max(merged[-1].end, interval.end) #如果能连接上我们就连
        
        return merged

大神写的,而我写的:

class Solution:
        
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """
        #检查len(intervals) 是否为1
        if len(intervals) == 1:
            return intervals
        #与大神一样的操作,可是为什么我写的就这么啰嗦和慢呢?
        intervals = sorted(intervals, key=lambda x: x.start)
        #我用startpos 去遍历,大神用for i
        startpos = 0
        jet = 0
        
        while startpos < len(intervals)-1:
            nextpos = startpos+1 #比较与下一项能不能
            if intervals[startpos].end < intervals[nextpos].start:
                startpos += 1
                continue
            #合并之后的maxbound
            maxbound = max(intervals[startpos].end, intervals[nextpos].end)
            #检查是否能有更多的nextpos加入
            while nextpos <= len(intervals)-1 and maxbound >= intervals[nextpos].start:
                nextpos += 1
                if nextpos > len(intervals) - 1:
                    #用jet防止越界
                    jet = 1
                maxbound = max(maxbound, intervals[nextpos-1].end)
            #防越界
            nextpos = min(nextpos, len(intervals) - 1)
            
            #如果越界
            if jet == 1:
                intervals.append(Interval(intervals[startpos].start, maxbound))
                intervals[startpos:nextpos+1] = []
            #如果没越界
            else:
                intervals.insert(nextpos, Interval(intervals[startpos].start, maxbound))
                intervals[startpos:nextpos] = []
            #开始融合的区间
            startpos += 1
        
        return intervals

首先长度是人家的好几倍,而且运行时间也是。。。真的需要锻炼自己完成算法的能力。就算你知道算法,你写的烂,你的代码能力还是不达标的。

为什么我这里写的这么差呢?

1.没有使用新的列表来储存答案,而是直接在原答案中修改,这直接导致我需要考虑是否越界,以及需要把某一段原区间修改为空。

2.找第一个合并区间与第二个或更后面的合并区间是分开的,这直接导致我的代码更加难读,还分成了2段。

3.不明白一步步融合的道理。简要的说就是,我融合的时候想一下找出所有的能融合的项,把它们合并在一起,但是为什么不一个个的合并呢?

总结,提高。路很长。

猜你喜欢

转载自blog.csdn.net/weixin_41958153/article/details/81161403