Java for LeetCode 057 Insert Interval

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

解题思路一:

参考Java for LeetCode 056 Merge Intervals思路一,去掉最外层循环即可,JAVA实现如下:

public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        if (newInterval == null)
            return intervals;
            int startIndex = 0, endIndex = 0;
            for (int j = 0; j < intervals.size(); j++) {
                if (newInterval.start > intervals.get(j).end) {
                    startIndex += 2;
                    endIndex += 2;
                    continue;
                }
                if (newInterval.end < intervals.get(j).start)
                    break;
                if (newInterval.start >= intervals.get(j).start)
                    startIndex++;
                if (newInterval.end > intervals.get(j).end) {
                    endIndex += 2;
                    continue;
                }
                if (newInterval.end >= intervals.get(j).start)
                    endIndex++;
                break;
            }
            if(startIndex==endIndex&&startIndex%2==0)
                intervals.add(startIndex/2,new Interval(newInterval.start,newInterval.end));
            else if(startIndex%2==0&&endIndex%2==0){
                intervals.get(startIndex/2).start=newInterval.start;
                intervals.get(startIndex/2).end=newInterval.end;
                for(int k=1;k<endIndex/2-startIndex/2;k++)
                intervals.remove(startIndex/2+1);
            }
            else if(startIndex%2==0&&endIndex%2!=0){
                intervals.get(startIndex/2).start=newInterval.start;
                intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
                for(int k=1;k<=endIndex/2-startIndex/2;k++)
                    intervals.remove(startIndex/2+1);
            }
            else if(startIndex%2!=0&&endIndex%2==0){
                intervals.get(startIndex/2).end=newInterval.end;
                for(int k=1;k<endIndex/2-startIndex/2;k++)
                    intervals.remove(startIndex/2+1);
            }
            else if(startIndex%2!=0&&endIndex%2!=0){
                intervals.get(startIndex/2).end=intervals.get(endIndex/2).end;
                for(int k=1;k<=endIndex/2-startIndex/2;k++)
                    intervals.remove(startIndex/2+1);
            }
        return intervals;
    }

解题思路二:

参考Java for LeetCode 056 Merge Intervals思路二,添加后重新排序即可,JAVA实现如下:

	public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        if (intervals == null)
        	return intervals;
        List<Interval> list = new ArrayList<Interval>();
        intervals.add(newInterval);
        Comparator<Interval> comparator = new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                if (o1.start == o2.start)
                    return o1.end - o2.end;
                return o1.start - o2.start;
            }
        };
        Collections.sort(intervals, comparator);
        for (Interval interval : intervals) {
            if (list.size() == 0 || list.get(list.size() - 1).end < interval.start)
                list.add(new Interval(interval.start, interval.end));
            else
                list.get(list.size() - 1).end = Math.max(interval.end, list.get(list.size() - 1).end);
        }
        return list;
    }
时间: 2024-10-11 10:57:10

Java for LeetCode 057 Insert Interval的相关文章

LeetCode --- 57. Insert Interval

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

[LeetCode][JavaScript]Insert Interval

https://leetcode.com/problems/insert-interval/ Insert Interval 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 t

【LeetCode】Insert Interval

Insert Interval 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], in

leetCode 57.Insert Interval (插入区间) 解题思路和方法

Insert Interval 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], i

LeetCode 57. Insert Interval / Add Interval (get total covered length)

57. Insert Interval 由于intervals已经有序,不需要排序.本题是要返回一个interval数组,所以并不需要对原数组进行改动. 方法一: 由于intervals有序,我们可以二分找到应该插入的位置,然后merge intervals即可. 时间复杂度 O(n) 方法二: 我们只需要找出overlap的部分,merge即可.别的没有overlap的部分保持不变. class Solution { public: vector<vector<int>> ins

LeetCode 57. Insert Interval 插入区间 (C++/Java)

题目: 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: Input: intervals = [[1,3],[6,9]], newInter

leetcode 57 Insert Interval ----- java

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 [

【LeetCode】Insert Interval 解题报告

[题目] 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 m

[LeetCode] 57. Insert Interval 解决思路

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 [