"The original in the SAE's blog, all transferred to the CSDN. "
Insert Interval-leetcode-javaPosted in2016/02/09
Topic:
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).
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]
.
This problem and Leetcode 56 merge intervals is a brother problem, this problem is to a series of interval interval, this interval is not overlapping each other, when inserting a interval after insertion if there is overlap, then merge.
Ideas:
First create a new result set LS, determine whether the original set intervals is empty, if it is empty, directly add NewInterval ls, return. Otherwise start to judge the situation, traverse the original set intervals, if the end of NewInterval is less than the start of the first I interval, add newinterval to the result set LS If the NewInterval start is greater than the end,z of the I, then add the I to the result set LS, in other cases, I need to compare the first and newinterval to merge. Start select the smallest of the two, and end select the largest of the two.
began to write their own time, walked into a misunderstanding, considering a lot of circumstances ah, in case this newinterval less than No. 0, the result end is not sure, can be ranked in the front, may need to merge two, may need to merge three, there is a start greater than the situation, the merger situation so much how to do ... Try to write a few if judgment statement thinking on the dog band. In fact, I think too far, although the comparison is also traversed, but I want to merge in a sudden situation to get out. The thinking is not clear. In fact, it should be in the loop to traverse the intervals, step by step considerations, each step only need to consider this is to add to the result set current or newinterval, or need to merge these two, cycle end experienced every step of the comparison, also came out results.
Finally loop to the last time, that is, Intervals.size ()-1, into the comparison of the three cases, no longer into the loop, and finally the newinterval added to the result set LS.
/**
* 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 {
Public list<interval> Insert (list<interval> intervals, Interval NewInterval) {
List<interval> ls = new arraylist<interval> ();
if (Intervals==null | | intervals.size () <1) {
Ls.add (newinterval); return LS;}
for (int i=0;i<intervals.size (); i++) {
Interval Curr = Intervals.get (i);
if (newinterval.end<curr.start) {
Ls.add (newinterval);
Ls.add (Curr); Start more This sentence, wrong answer
for (int j=i;j<intervals.size (); j + +) {
Ls.add (Intervals.get (j));
}
break;
}else if (newinterval.start>curr.end) {
Ls.add (curr);
} else{
Newinterval.start=math.min (curr.start,newinterval.start);
Newinterval.end=math.max (Curr.end,newinterval.end);
}
if (I==intervals.size ()-1) {
Ls.add (newinterval);
}
}
return ls;
}
}
Posted in leetcode| tags are java, Leetcode| Post a reply
Insert Interval-leetcode-java