GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/python/056_Merge_Intervals.py at master · iamsathish/leetcode Expand file tree/ Copy path056_Merge_Intervals.pyMore file actions 36 lines (34 loc) · 1.06 KB / Copy path056_Merge_Intervals.pyFile metadata and controls36 lines (34 loc) · 1.06 KB 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. |