国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode/LintCode] Merge Intervals

gougoujiang / 1902人閱讀

摘要:方法上沒太多難點(diǎn),先按所有區(qū)間的起點(diǎn)排序,然后用和兩個(gè)指針,如果有交集進(jìn)行操作,否則向后移動(dòng)。由于要求的,就對(duì)原數(shù)組直接進(jìn)行操作了。時(shí)間復(fù)雜度是的時(shí)間。

Problem

Given a collection of intervals, merge all overlapping intervals.

Example
Given intervals => merged intervals:

[                     [
  [1, 3],               [1, 6],
  [2, 6],      =>       [8, 10],
  [8, 10],              [15, 18]
  [15, 18]            ]
]
Challenge
O(n log n) time and O(1) extra space.
Note

方法上沒太多難點(diǎn),先按所有區(qū)間的起點(diǎn)排序,然后用pre和cur兩個(gè)指針,如果有交集進(jìn)行merge操作,否則pre向后移動(dòng)。由于要求O(1)的space,就對(duì)原數(shù)組直接進(jìn)行操作了。
時(shí)間復(fù)雜度O(nlogn)Collections.sort()的時(shí)間。for循環(huán)是O(n)
這道題有兩個(gè)點(diǎn):
學(xué)會(huì)使用Collections.sort(object, new Comparator(){})進(jìn)行排序;
對(duì)于要進(jìn)行更改的數(shù)組而言,其一,for循環(huán)不要用for (a: A)的形式,會(huì)出現(xiàn)ConcurrentModificationException的編譯錯(cuò)誤,文檔是這樣解釋的:it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. 其二,對(duì)intervalscur元素進(jìn)行刪除操作之后,對(duì)應(yīng)的index i要減去1。

Solution Update 2018-9
class Solution {
    public List merge(List intervals) {
        if (intervals == null || intervals.size() < 2) return intervals;
        intervals.sort((i1, i2) -> i1.start-i2.start);
        List res = new ArrayList<>();
        int start = intervals.get(0).start, end = intervals.get(0).end; //use two variables to maintain prev bounds
        for (Interval interval: intervals) {                            //iterate the interval list
            if (interval.start > end) {                                 //if current interval not overlapping with prev:
                res.add(new Interval(start, end));                      //1. add prev to result list
                start = interval.start;                                 //2. update prev bounds
                end = interval.end;
            }
            else end = Math.max(end, interval.end);                     //else just update prev end bound
        }
        res.add(new Interval(start, end));                              //add the prev which was updated by the last interval
        return res;
    }
}
in-place
class Solution {
    public List merge(List intervals) {
        if (intervals == null || intervals.size() < 2) return intervals;
        intervals.sort((i1, i2) -> i1.start-i2.start);
        Interval pre = intervals.get(0);
        for (int i = 1; i < intervals.size(); i++) {
            Interval cur = intervals.get(i);
            if (cur.start > pre.end) pre = cur;
            else {
                pre.end = Math.max(pre.end, cur.end);
                intervals.remove(cur);
                i--;
            }
        }
        return intervals;
    }
}

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

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/65539.html

相關(guān)文章

  • [Leetcode] Merge Intervals and Insert Interval 合并間

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

    antyiwei 評(píng)論0 收藏0
  • 【LC總結(jié)】Intervals題目 (Insert/Merge/Number of Airplane

    摘要:忘了這題怎么做,汗顏無地。邊界用記錄每個(gè)時(shí)刻的飛行數(shù)目。對(duì)于某一時(shí)刻,起飛和降落同時(shí)發(fā)生,只計(jì)算一次。先強(qiáng)勢(shì)插入,再 Merge Intervals Problem Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals: ...

    Achilles 評(píng)論0 收藏0
  • leetcode56 Merge Intervals

    摘要:題目要求輸入一系列區(qū)間,將出現(xiàn)交叉的區(qū)間合并起來,并將合并后的區(qū)間返回。將這兩個(gè)數(shù)組分別排序可以得到和。也就是說,合并后的結(jié)果和是等價(jià)的。舉個(gè)栗子,輸入為,先判斷是否是獨(dú)立區(qū)間,可見可以和合并,則將修改為。 題目要求 Given a collection of intervals, merge all overlapping intervals. For example, Given...

    shmily 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0
  • leetcode--57--Insert Interval

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

    kycool 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<