# Hard-题目28：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 [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].

/**
* 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) {
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++;
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])
else if(!x[start] && !x[start-1])
}

}
return list;
}
}

9ms,24.58%,5ms,26.17%

• 本文已收录于以下专栏：

## LeetCode --- 57. Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa...
• 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
• 2015年07月15日 14:11
• 732

## 57 Insert Interval

• 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
• 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
• 2017年07月03日 17:16
• 123

## [LeetCode] 57. Insert Interval

leetcode
• 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
• 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
• 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
• 2017年11月14日 16:09
• 53

## Leetcode 57. Insert Interval

• galaxy_wolf
• 2016年04月29日 16:26
• 110

举报原因： 您举报文章：Hard-题目28：57. Insert Interval 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)