当前位置:网站首页>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
边栏推荐
- Form custom verification rules
- 2022-2028 global encryption software industry research and trend analysis report
- 2022-2028 global aluminum beverage can coating industry research and trend analysis report
- Download and use of the super perfect screenshot tool snipaste
- Continuous assignment of Verilog procedure
- Use usedeferredvalue for asynchronous rendering
- MSI announced that its motherboard products will cancel all paper accessories
- In depth interpretation of pytest official documents (26) customized pytest assertion error information
- /silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult
- MongoDB非關系型數據庫
猜你喜欢
Form custom verification rules
JS <2>
C reflection practice
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Golang configure export goprivate to pull private library code
Render header usage of El table
Mongodb base de données non relationnelle
表单自定义校验规则
[C Advanced] brother Peng takes you to play with strings and memory functions
[golang] leetcode intermediate bracket generation & Full Permutation
随机推荐
ZABBIX API creates hosts in batches according to the host information in Excel files
A list of job levels and salaries in common Internet companies. Those who have conditions must enter big factories. The salary is really high
About DNS
2022-2028 global nano abrasive industry research and trend analysis report
Learn PWN from CTF wiki - ret2shellcode
浅谈线程池相关配置
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Just a few simple steps - start playing wechat applet
2022-2028 global military computer industry research and trend analysis report
Spark Tuning
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Baohong industry | what misunderstandings should we pay attention to when diversifying investment
32, 64, 128 bit system
GB/T-2423. XX environmental test documents, including the latest documents
Delphi xe10.4 installing alphacontrols15.12
Verilog 过程连续赋值
Competition and adventure burr
SAML2.0 笔记(一)
4. Find the median of two positive arrays