GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/056_Merge_Intervals.py at master · liuxiang2020/leetcode

Expand file tree

/

056_Merge_Intervals.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
36 lines (34 loc) · 1.06 KB
/

056_Merge_Intervals.py

Copy path

File metadata and controls

36 lines (34 loc) · 1.06 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Definition for an interval.
# class Interval(object):
# def __init__(self, s=0, e=0):
# self.start = s
# self.end = e
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
if intervals is None:
return
ls = len(intervals)
if ls <= 1:
return intervals
# sort by start
intervals.sort(key=lambda x: x.start)
pos = 0
while pos < len(intervals) - 1:
# check overlap
if intervals[pos].end >= intervals[pos + 1].start:
next = intervals.pop(pos + 1)
# check next is overlap or totally covered by pos
if next.end > intervals[pos].end:
intervals[pos].end = next.end
# print [(t.start, t.end) for t in intervals], pos
else:
pos += 1
return intervals
if __name__ == '__main__':
# begin
s = Solution()
print s.merge([[1,3],[2,6],[8,10],[15,18]])
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.