本文主要是介绍LEETCODE-DAY36,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
title: LEETCODE-DAY36
date: 2024-03-27 18:49:59
tags:
今日内容:435. 无重叠区间、763.划分字母区间、56. 合并区间
T1
思路类似DAY35 T3
class Solution:def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:intervals.sort()j=0for i in range(len(intervals)):if i>0 and intervals[i][0]<intervals[i-1][1]:#因为要使j尽可能小,故把较大的上界对应的区间removeintervals[i][1]=min(intervals[i][1],intervals[i-1][1])j+=1return j
AC
T2
class Solution:def partitionLabels(self, s: str) -> List[int]:last_occurrence = {} # 存储每个字符最后出现的位置for i, ch in enumerate(s):last_occurrence[ch] = iresult = []start = 0end = 0for i, ch in enumerate(s):end = max(end, last_occurrence[ch]) # 找到当前字符出现的最远位置if i == end: # 如果当前位置是最远位置,表示可以分割出一个区间result.append(end - start + 1)start = i + 1return result
T3
class Solution:def merge(self, intervals: List[List[int]]) -> List[List[int]]:res=list()intervals.sort()for i in range(len(intervals)):if i>0 and intervals[i][0]<=intervals[i-1][1]:intervals[i][0]=intervals[i-1][0]intervals[i][1]=max(intervals[i][1],intervals[i-1][1])elif i>0 and intervals[i][0]>intervals[i-1][1]:res.append(intervals[i-1])return res
输入
intervals =
[[1,3],[2,6],[8,10],[15,18]]
输出
[[1,6],[8,10]]
预期结果
[[1,6],[8,10],[15,18]]
输入
intervals =
[[1,4],[4,5]]
输出
[]
预期结果
[[1,5]]
class Solution:def merge(self, intervals: List[List[int]]) -> List[List[int]]:res=list()intervals.sort()for i in range(len(intervals)):if i>0 and intervals[i][0]<=intervals[i-1][1]:intervals[i][0]=intervals[i-1][0]intervals[i][1]=max(intervals[i][1],intervals[i-1][1])elif i>0 and intervals[i][0]>intervals[i-1][1]:res.append(intervals[i-1])res.append(intervals[i])return res
AC
这篇关于LEETCODE-DAY36的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!