Same idea, use fewer lines, python code


  • 0
    T
    class Solution:
    # @param intervals, a list of Interval
    # @return a list of Interval
    def merge(self, intervals):
        intervals.sort(key=lambda x: x.start)
        res = []
        for i in intervals:
            if res and i.start <= res[-1].end:
                res[-1].end = max(i.end, res[-1].end)
            else:
                res.append(i)
        return res
    

    Don't write this

            if intervals == []:
    

    Just

            if not intervals:

  • 0
    Z

    Could you please provide some examples and explain how to test them? Thanks a lot!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.