leetcode——57.插入区间

public int[][] insert(int[][] intervals, int[] newInterval) {
        int newStart = newInterval[0],newEnd = newInterval[1];
        int idx = 0;
        int n = intervals.length;
        LinkedList<int[]> output = new LinkedList<>();
        while (idx<n && newStart>intervals[idx][0]){
            output.add(intervals[idx++]);
        }
        int[] interval = new int[2];
        if(output.isEmpty() || output.getLast()[1]<newStart){
            output.add(newInterval);
        }else{
            interval = output.removeLast();
            interval[1] = Math.max(interval[1],newEnd);
            output.add(interval);
        }
        
        while (idx<n){
            interval = intervals[idx++];
            int start = interval[0],end = interval[1];
            if(output.getLast()[1]<start) {
            } else{
                interval = output.removeLast();
                interval[1] = Math.max(interval[1],end);
            }
            output.add(interval);
        }
        return output.toArray(new int[output.size()][2]);
    }

 

posted @ 2020-09-02 17:32  欣姐姐  阅读(170)  评论(0编辑  收藏  举报