avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • Resume
  • Archives
  • Categories
  • Photos
  • Music



{{ date }}

{{ time }}

avatar
Siz Long

My name is Siz. I am a computer science graduate student specializing in backend development with Golang and Python, seeking opportunities in innovative tech projects. My personal website is me.longsizhuo.com .Connect with me on LinkedIn: https://www.linkedin.com/in/longsizhuo/.

  • 主页
  • Resume
  • Archives
  • Categories
  • Photos
  • Music

56. Merge Intervals

  2025-03-02        
字数统计: 144字   |   阅读时长: 1min

题目:

截屏2024-05-27 下午9.49.26.png

思想:

就排序,排完之后,两两比较后merge.

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from typing import List

class Solution:
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
# 先按区间的起始位置进行排序
intervals.sort(key=lambda x: x[0])

merged = []
for interval in intervals:
# 如果merged列表为空,或者当前区间不与merged列表中的最后一个区间重叠,直接添加到merged列表
if not merged or merged[-1][1] < interval[0]:
merged.append(interval)
else:
# 否则我们就合并当前区间和merged列表中的最后一个区间
merged[-1][1] = max(merged[-1][1], interval[1])

return merged
  • Python
  • 数组
  • 排序

扫一扫,分享到微信

微信分享二维码
Windows7+SSH
9021_TUT_2_25T1
目录
  1. 1. 题目:
  2. 2. 思想:
  3. 3. 代码:

150 篇 | 131.7k
次 | 人
这里自动载入天数这里自动载入时分秒
2022-2025 loong loong | 新南威尔士龙龙号