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

資訊專欄INFORMATION COLUMN

merge Interval

terro / 2077人閱讀

For example,
Given [1,3],[2,6],[8,10],[15,18],
return [1,6],[8,10],[15,18].
# 1 based on start  26ms
/**
 * Definition for an interval.
 * public class Interval {
 *     int start;
 *     int end;
 *     Interval() { start = 0; end = 0; }
 *     Interval(int s, int e) { start = s; end = e; }
 * }
 */
public class Solution {
    public List merge(List intervals) {
        List res = new ArrayList();
        if(intervals == null || intervals.size() <= 1) return intervals;
        Collections.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return a.start - b.start;
            }
        });
        
        int start = intervals.get(0).start;
        int end = intervals.get(0).end;
        
        for(Interval interval : intervals){
            if(interval.start <= end){
                end = Math.max(end, interval.end);
            } else {
                res.add(new Interval(start, end));
                start = interval.start;
                end = interval.end;
            }
        }
        res.add(new Interval(start, end));
   
        return res;
    }
}
# 2 based on end  24ms
public class Solution {
    public List merge(List intervals) {
        List res = new ArrayList();
        if(intervals == null || intervals.size() <= 1) return intervals;
        Collections.sort(intervals, new Comparator(){
            public int compare(Interval a, Interval b){
                return b.end - a.end;
            }
        });
        
        Interval last = intervals.get(0);
        Interval cur = null;
        for(int i = 1; i < intervals.size(); i++){
            cur = intervals.get(i);
            if(last.start > cur.end){
                res.add(last);
                last = cur;
            } else {
                last.start = Math.min(cur.start, last.start);
            }
        }
        res.add(last);
        
        Collections.reverse(res);
        
        return res;
    }
}
# number of meeting rooms, 拆分start,end  20ms
public class Solution {
    public List merge(List intervals) {
        int n = intervals.size();
        int[] starts = new int[n];
        int[] ends = new int[n];
        for(int i=0; i res = new ArrayList();
        for(int i=0, j=0; i ends[i]){  // next meeting starts after this ends, cant merge
                res.add(new Interval(starts[j], ends[i]));
                j = i + 1;
            }
        }
        return res;
    }
}

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

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

相關(guān)文章

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

    摘要:我們只要把所有和該有重疊的合并到一起就行了。最后把前半部分的列表,合并后的大和后半部分的列表連起來(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...

    antyiwei 評(píng)論0 收藏0
  • [LeetCode/LintCode] Merge Intervals

    摘要:方法上沒(méi)太多難點(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...

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

    摘要:?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...

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

    摘要:忘了這題怎么做,汗顏無(wú)地。邊界用記錄每個(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ū)間合并起來(lái),并將合并后的區(qū)間返回。將這兩個(gè)數(shù)組分別排序可以得到和。也就是說(shuō),合并后的結(jié)果和是等價(jià)的。舉個(gè)栗子,輸入為,先判斷是否是獨(dú)立區(qū)間,可見(jiàn)可以和合并,則將修改為。 題目要求 Given a collection of intervals, merge all overlapping intervals. For example, Given...

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

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

0條評(píng)論

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