成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

leetcode57. Insert Interval

Yuanf / 376人閱讀

摘要:題目要求給定一組順序排列且相互之間沒有重疊的區(qū)間,輸入一個區(qū)間,將它插入到當(dāng)前的區(qū)間數(shù)組中,并且將需要合并的區(qū)間合并,之后返回插入并且合并后的區(qū)間。我們將這三個類型的區(qū)間分別標(biāo)注為類型,類型,類型。

題目要求
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.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

給定一組順序排列且相互之間沒有重疊的區(qū)間,輸入一個區(qū)間,將它插入到當(dāng)前的區(qū)間數(shù)組中,并且將需要合并的區(qū)間合并,之后返回插入并且合并后的區(qū)間。

思路和代碼

任何一個區(qū)間數(shù)組中的區(qū)間可以劃分為三個類型,位于需要被合并的區(qū)間的前面的區(qū)間,需要被合并的區(qū)間,位于需要被合并的區(qū)間后面的區(qū)間。我們將這三個類型的區(qū)間分別標(biāo)注為類型1,類型2,類型3。
區(qū)間類型1: 當(dāng)前區(qū)間的最大值小于插入?yún)^(qū)間的最大值
區(qū)間類型3: 當(dāng)前區(qū)間的最小值大于插入?yún)^(qū)間的最大值
區(qū)間類型2: 判斷比較復(fù)雜,可以通過非區(qū)間類型1和區(qū)間類型3來歸類。在遇到區(qū)間類型二時,要更新插入?yún)^(qū)間的最大值和最小值

代碼實現(xiàn)如下:
方法一:

    public List insert(List intervals, Interval newInterval) {
        List result = new ArrayList();
        int index = 0;
        while(index

方法二:

    public List insert2(List intervals, Interval newInterval) {
        List result = new ArrayList();
        for(Interval temp : intervals){
            if(newInterval==null || temp.end < newInterval.start){
                result.add(temp);
            }else if(temp.start > newInterval.end){
                result.add(newInterval);
                result.add(temp);
                newInterval = null;
            }else{
                newInterval.start = Math.min(newInterval.start, temp.start);
                newInterval.end = Math.max(newInterval.end, temp.end);
            }
        }
        return result;
    }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/70145.html

相關(guān)文章

  • leetcode--57--Insert Interval

    摘要:問題描述分析這道題的關(guān)鍵在于理解問題,抽取原型,理解中間可以部分如何界定,以及非部分如何進行追加。需要注意的是循環(huán)到最后一個元素和在最后一個元素的區(qū)別。 問題描述: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You m...

    kycool 評論0 收藏0
  • 57. Insert Interval

    57. Insert Interval 題目鏈接:https://leetcode.com/problems... public class Solution { public List insert(List intervals, Interval newInterval) { // skip intervals which not overlap with new on...

    kid143 評論0 收藏0
  • [Leetcode] Merge Intervals and Insert Interval 合并間

    摘要:我們只要把所有和該有重疊的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表連起來,就是結(jié)果了。 Merge Intervals 最新更新請見 https://yanjia.me/zh/2019/02/... Given a collection of intervals, merge all overlapping intervals.For example, Gi...

    antyiwei 評論0 收藏0
  • [LeetCode] Insert Interval

    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....

    Jonathan Shieber 評論0 收藏0
  • leetcode436. Find Right Interval

    摘要:題目要求假設(shè)一個二維的整數(shù)數(shù)組中每一行表示一個區(qū)間,每一行的第一個值表示區(qū)間的左邊界,第二個值表示區(qū)間的右邊界。 題目要求 Given a set of intervals, for each of the interval i, check if there exists an interval j whose start point is bigger than or equal ...

    robin 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<