当前位置:网站首页>Merge interval, linked list, array
Merge interval, linked list, array
2022-07-02 03:18:00 【huaMinPython】
In array intervals Represents a set of intervals , The single interval is intervals[i] = [starti, endi] . Please merge all overlapping intervals , And return a non overlapping interval array , The array needs to cover exactly all the intervals in the input .
Example 1:
Input :intervals = [[1,3],[2,6],[8,10],[15,18]]
Output :[[1,6],[8,10],[15,18]]
explain : Section [1,3] and [2,6] overlap , Combine them into [1,6].
Example 2:
Input :intervals = [[1,4],[4,5]]
Output :[[1,5]]
explain : Section [1,4] and [4,5] Can be regarded as overlapping interval .
# Merge range
def sortIntervals(intervals): # Write function Insertion sort
for i in range(1,len(intervals)):
for j in range(i):
if intervals[i][0]<intervals[j][0]:
tmp=intervals[i]
del intervals[i]
intervals.insert(j,tmp)
return intervals
def merge(intervals): # Write function , Merge range
sortIntervals(intervals) # The first step is to sort, That is to give intervals Sort
for i in range(1,len(intervals)): # The second step is to merge , And use 【0,0】 placeholder
if intervals[i][0] <= intervals[i-1][1]:
intervals[i-1][1]=intervals[i][1]
intervals[i]=[0,0]
while True: # The third step Delete 【0,0】
if [0,0] in intervals:
intervals.remove([0,0])
else:
break
return intervals
print(merge([[1,3],[8,10],[2,6],[15,18]]))
print(merge([[1,4],[4,5]]))
print(merge([[1,4],[4,5],[6,12],[11,15]]))
Insertion sort
【0,0】 placeholder
remove Method
边栏推荐
- Redis set command line operation (intersection, union and difference, random reading, etc.)
- Global and Chinese markets for electronic laryngoscope systems 2022-2028: Research Report on technology, participants, trends, market size and share
- Mathematical calculation in real mode addressing
- Verilog 状态机
- MongoDB非关系型数据库
- 初出茅庐市值1亿美金的监控产品Sentry体验与架构
- Global and Chinese market of gynaecological health training manikin 2022-2028: Research Report on technology, participants, trends, market size and share
- Named block Verilog
- Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
- Framing in data transmission
猜你喜欢

Retrofit's callback hell is really vulnerable in kotlin synergy mode

West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials

verilog 并行块实现

2022-2028 global military computer industry research and trend analysis report

Redis cluster

PHP notes - use Smarty to set public pages (include, if, else, variable settings)

SAML2.0 notes (I)

Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性

2022-2028 global encryption software industry research and trend analysis report

C shallow copy and deep copy
随机推荐
Use blocking or non blocking for streamline
跟着CTF-wiki学pwn——ret2shellcode
Form custom verification rules
Xiaomi, a young engineer, was just going to make soy sauce
Halcon image rectification
Gradle 笔记
ORA-01547、ORA-01194、ORA-01110
Continuous assignment of Verilog procedure
QT environment generates dump to solve abnormal crash
[C Advanced] brother Peng takes you to play with strings and memory functions
Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
Detailed explanation of the difference between Verilog process assignment
Verilog 过程连续赋值
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
2022-2028 global encryption software industry research and trend analysis report
Render header usage of El table
Aaaaaaaaaaaa
Load different fonts in QML
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
MongoDB非關系型數據庫