LintCode:合并区间
来源:互联网 发布:美工要学哪些 编辑:程序博客网 时间:2024/06/13 15:38
LintCode:合并区间
先排序,再逐个合并
"""Definition of Interval.class Interval(object): def __init__(self, start, end): self.start = start self.end = end"""class Solution: # @param intervals, a list of Interval # @return a list of Interval def merge(self, intervals): # write your code here if len(intervals) == 0 or len(intervals) <= 1: return intervals self.L = intervals self.quick_sort(self.L, 0, len(intervals)-1) ans = [] while len(self.L) > 1: while len(self.L) > 1 and self.L and self.L[0].end >= self.L[1].start: if self.L[0].end <= self.L[1].end:# print 'Ok' self.L[0].end = self.L[1].end self.L.remove(self.L[1]) else: self.L.remove(self.L[1]) ans.append(self.L[0]) self.L.remove(self.L[0]) if self.L: ans.append(self.L[0]) return ans def quick_sort(self, L, low, high): if low >= high: return i = low j = high tmp = L[i] while i < j: while i < j and L[j].start >= tmp.start: j -= 1 L[i] = L[j] while i < j and L[i].start <= tmp.start: i += 1 L[j] = L[i] L[i] = tmp self.quick_sort(L, low, i-1) self.quick_sort(L, i+1, high)
0 0
- lintcode-156-合并区间
- LintCode- 合并区间
- LintCode:合并区间
- 合并区间-lintcode
- lintcode,合并区间
- LintCode【合并区间】
- LintCode-合并区间
- lintcode 合并区间
- lintcode 合并区间
- 合并区间-LintCode
- LintCode 合并区间
- LintCode 合并区间
- lintcode[156]:合并区间
- 合并区间-LintCode
- Lintcode——合并区间
- lintCode-给出若干闭合区间,合并所有重叠的部分。
- 区间合并
- 合并区间
- CentOS 6.x启动时网卡eth0未激活
- Android 之BlockingQueue
- 架构之美读书笔记[1]---对"巴洛克式"(巴罗克式)的理解
- 地理编码和反地理编码
- java回忆录—类与对象(万物皆对象)
- LintCode:合并区间
- SDK API IDE专业术语解释
- 快速排序-quicksort
- 这么完美的梗,只有程序员才懂
- APP逆向(上)
- 深入Linux PAM体系结构
- POJ 2536 解题报告
- 4-5 链式表操作集 (20分)
- HDU 5731 2016多校Contest 1 F题【轮廓线DP+容斥原理】