8000 Total Runtime: 557 ms · FantasyNew/LintCode_Python@697eeaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 697eeaa

Browse files
committed
1 parent 3774e10 commit 697eeaa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Insert_Interval.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Interval(object):
2+
def __init__(self, start, end):
3+
self.start = start
4< 8000 code class="diff-text syntax-highlighted-line addition">+
self.end = end
5+
6+
7+
class Solution:
8+
"""
9+
Insert a new interval into a sorted non-overlapping interval list.
10+
@param intevals: Sorted non-overlapping interval list
11+
@param newInterval: The new interval.
12+
@return: A new sorted non-overlapping interval list with the new interval.
13+
"""
14+
def insert(self, intervals, newInterval):
15+
results, position = [], 0
16+
17+
for i in intervals:
18+
start, end = i.start, i.end
19+
if newInterval.start > end:
20+
results.append(i)
21+
position += 1
22+
elif newInterval.end < start:
23+
results.append(i)
24+
else:
25+
newInterval.start = min(start, newInterval.start)
26+
newInterval.end = max(end, newInterval.end)
27+
28+
results.insert(position, newInterval)
29+
return results

0 commit comments

Comments
 (0)
0