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 necessa...
  • makuiyu
  • makuiyu
  • 2015年03月14日 23:10
  • 1315

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

Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the interva...
  • xygy8860
  • xygy8860
  • 2015年07月15日 14:11
  • 732

57 Insert Interval

题目链接:https://leetcode.com/problems/insert-interval/题目:Given a set of non-overlapping intervals, inse...
  • ChilseaSai
  • ChilseaSai
  • 2015年10月11日 17:23
  • 444

[Java]LeetCode57 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
  • fumier
  • fumier
  • 2015年08月18日 20:52
  • 851

LeetCode | 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
  • pku_Coder
  • pku_Coder
  • 2017年07月03日 17:16
  • 123

[LeetCode] 57. Insert Interval

leetcode
  • hiimdaosui
  • hiimdaosui
  • 2016年07月26日 16:34
  • 99

leetcode 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
  • wl0627
  • wl0627
  • 2016年09月19日 20:38
  • 85

LeetCode 57. Insert Interval

#include #include using namespace std; /* Given a set of non-overlapping intervals, insert a ne...
  • github_34333284
  • github_34333284
  • 2016年05月09日 05:13
  • 165

LeetCode||57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
  • Lingfu74
  • Lingfu74
  • 2017年11月14日 16:09
  • 53

Leetcode 57. Insert Interval

问题https://leetcode.com/problems/insert-interval/解法要插入的段 newInterval 与原有的intervals 会相互重叠一部分。 首先找到重叠的...
  • galaxy_wolf
  • galaxy_wolf
  • 2016年04月29日 16:26
  • 110
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目28:57. Insert Interval
举报原因:
原因补充:

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