Hard-题目28:57. Insert Interval

原创 2016年05月31日 23:39:52

题目原文:
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].
题目大意:
给出一个区间集合,插入一个区间并化简。
题目分析:
跟前面的Hard-题目22:56. Merge Intervals是一回事,但是数据强了一些,要把数组开到40000.
源码:(language:java)

/**
 * 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 {
    private class IntervalComparator implements Comparator<Interval> {
        @Override
        public int compare(Interval o1, Interval o2) {
            // TODO Auto-generated method stub
            if(o1.start<o2.start)
                return -1;
            else if(o1.start>o2.start)
                return 1;
            else {
                if(o1.end<o2.end)
                    return -1;
                else if(o1.end>o2.end)
                    return 1;
                else {
                    return 0;
                }
            }
        }

    }
    public List<Interval> insert(List<Interval> intervals, Interval newInterval) {
        intervals.add(newInterval);
        List<Interval> temp = merge(intervals);
        Collections.sort(temp, new IntervalComparator());
        return temp;
    }
    public List<Interval> merge(List<Interval> intervals) {
        boolean[] x = new boolean[40000];
        boolean[] dots = new boolean[40000];
        int max = Integer.MIN_VALUE,min = Integer.MAX_VALUE;
        List<Interval> list = new ArrayList<Interval>();
        for(Interval interval : intervals) {
            for(int i=interval.start;i<interval.end;i++) {
                x[i]=true;
                if(i<min)
                    min=i;
                if(i>max)
                    max=i;
            }
        }

        int i = min;
        while(i<=max) {
            if(!x[i]) {
                i++;
                continue;
            }
            int start=i,end=start;
            while(x[end])
                end++;
            list.add(new Interval(start,end));
            i=end;

        }
        for(Interval interval : intervals) {
            int start = interval.start,end = interval.end;
            if(start==end && !dots[interval.start]) {
                dots[start] = true;
                if(start==0 && !x[0])
                    list.add(new Interval(start, end));
                else if(!x[start] && !x[start-1])
                    list.add(new Interval(start, end));
            }

        }
        return list;
    }
}

成绩:
9ms,24.58%,5ms,26.17%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

LeetCode(57) Insert Interval

题目Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if neces...

【leetcode】Array——Insert Interval(57)

题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece...

LeetCode_57---Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

[leetcode] 57.Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if ne...

Leetcode-Array-57(insert Interval)

当然,leetcode拥有良好的函数处理平台,但有时候我想知道如何编写出来一个完整的程序,现在我给出一种编写方法吧,请各位大神多多指教

leetcode题解-57. Insert Interval

题目:Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if nece...

lkeetcode 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...

LeetCode 57. Insert Interval/56. Merge Intervals

1. 题目要求 Given a set of non-overlapping intervals, insert a new interval into the intervals (merge ...

[leetcode] 57.Insert Interval

题目: Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if ne...

Leetcode57 Insert Interval

Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)