57. Insert Interval
題目鏈接:https://leetcode.com/problems...
public class Solution { public Listinsert(List intervals, Interval newInterval) { // skip intervals which not overlap with new one // intervals.get(i).end < newInterval.start List res = new ArrayList(); int i = 0, n = intervals.size(); while(i < n && intervals.get(i).end < newInterval.start) res.add(intervals.get(i++)); // merge the overlap part // intervals.get(i).start <= newInterval.end while(i < n && intervals.get(i).start <= newInterval.end) { newInterval.start = Math.min(newInterval.start, intervals.get(i).start); newInterval.end = Math.max(newInterval.end, intervals.get(i).end); i++; } res.add(newInterval); // add last intervals without overlap with new one while(i < n) res.add(intervals.get(i++)); return res; } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/69887.html
摘要:?jiǎn)栴}描述分析這道題的關(guān)鍵在于理解問(wèn)題,抽取原型,理解中間可以部分如何界定,以及非部分如何進(jìn)行追加。需要注意的是循環(huán)到最后一個(gè)元素和在最后一個(gè)元素的區(qū)別。 問(wèn)題描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...
摘要:題目要求給定一組順序排列且相互之間沒(méi)有重疊的區(qū)間,輸入一個(gè)區(qū)間,將它插入到當(dāng)前的區(qū)間數(shù)組中,并且將需要合并的區(qū)間合并,之后返回插入并且合并后的區(qū)間。我們將這三個(gè)類型的區(qū)間分別標(biāo)注為類型,類型,類型。 題目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge...
摘要:我們只要把所有和該有重疊的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表連起來(lái),就是結(jié)果了。 Merge Intervals 最新更新請(qǐng)見(jiàn) https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...
Problem Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times....
閱讀 3338·2021-11-18 10:02
閱讀 2088·2021-09-22 10:54
閱讀 3039·2019-08-30 15:43
閱讀 2648·2019-08-30 13:22
閱讀 1629·2019-08-29 13:57
閱讀 1117·2019-08-29 13:27
閱讀 805·2019-08-26 14:05
閱讀 2596·2019-08-26 13:30