Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0d7cc68

Browse files
author
hj.tian
committed
feat: add leetcode75 day32_57
feat: add leetcode75 day32_57
1 parent a7725b5 commit 0d7cc68

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

‎leetcode75/day32_57.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
6+
"""
7+
根据结果来模拟
8+
return intervals[:start] + [[left, right]] + intervals[end:]
9+
即求 start, end, left, right 这四个值
10+
模拟 + 二分查找
11+
"""
12+
left, right = newInterval[0], newInterval[1]
13+
start = self.binary_search(nums=[interval[0] for interval in intervals], target=left)
14+
if start > 0 and left <= intervals[start - 1][1]:
15+
left = intervals[start - 1][0]
16+
start -= 1
17+
end = start
18+
while end < len(intervals) and intervals[end][1] <= right:
19+
end += 1
20+
if end < len(intervals) and intervals[end][0] <= right:
21+
right = intervals[end][1]
22+
end += 1
23+
return intervals[:start] + [[left, right]] + intervals[end:]
24+
25+
def binary_search(self, nums, target):
26+
# [)
27+
left, right = 0, len(nums)
28+
while left < right:
29+
mid = left + (right - left) // 2
30+
if nums[mid] == target:
31+
return mid
32+
elif nums[mid] < target:
33+
left = mid + 1
34+
else:
35+
right = mid
36+
return left
37+
38+
39+
if __name__ == "__main__":
40+
# 中间,需要判断左右区间
41+
assert Solution().insert(intervals=[[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], newInterval=[4, 8]) == [
42+
[1, 2],
43+
[3, 10],
44+
[12, 16],
45+
]
46+
# 插在左区间, 不需要合并
47+
assert Solution().insert(intervals=[[6, 9]], newInterval=[2, 5]) == [[2, 5], [6, 9]]
48+
# 插在右区间, 不需要合并
49+
assert Solution().insert(intervals=[[2, 5]], newInterval=[6, 9]) == [[2, 5], [6, 9]]
50+
# 插入
51+
assert Solution().insert(intervals=[], newInterval=[5, 7]) == [[5, 7]]
52+
# 插在左区间, 需要合并
53+
assert Solution().insert(intervals=[[6, 9]], newInterval=[2, 6]) == [[2, 9]]
54+
# 插在右区间, 需要合并
55+
assert Solution().insert(intervals=[[2, 6]], newInterval=[6, 9]) == [[2, 9]]
56+
# 中间, 不需要合并
57+
assert Solution().insert(intervals=[[1, 3], [6, 9]], newInterval=[4, 5]) == [[1, 3], [4, 5], [6, 9]]
58+
# 覆盖
59+
assert Solution().insert(intervals=[[1, 5]], newInterval=[0, 4]) == [[0, 5]]
60+
assert Solution().insert(intervals=[[1, 5]], newInterval=[1, 2]) == [[1, 5]]
61+
assert Solution().insert(intervals=[[1, 5]], newInterval=[1, 7]) == [[1, 7]]
62+
# 中间, 需要判断右区间
63+
assert Solution().insert(intervals=[[1, 3], [6, 9]], newInterval=[2, 5]) == [[1, 5], [6, 9]]
64+
# 中间, 需要判断左区间
65+
assert Solution().insert(intervals=[[1, 3], [6, 9]], newInterval=[4, 8]) == [[1, 3], [4, 9]]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /