码迷,mamicode.com
首页 > 编程语言 > 详细

Java for LeetCode 057 Insert Interval

时间:2015-05-15 21:13:28      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }

 

Java for LeetCode 057 Insert Interval

标签:

原文地址:http://www.cnblogs.com/tonyluis/p/4506704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!