-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
70 lines (70 loc) · 2.28 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* 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) {
int s = newInterval.start;
int e = newInterval.end;
if (intervals.size()==0) {
intervals.add(newInterval);
return intervals;
}
List<Interval> answerList = new ArrayList<Interval>();
int ns=0;
int ne=0;
boolean beginFlag=true;
boolean endFlag=true;
for (int i = 0; i < intervals.size(); i++) {
if (endFlag&&beginFlag) {
if (intervals.get(i).start>=s) {
ns=s;
beginFlag=false;
}else if (intervals.get(i).end>=s) {
if (intervals.get(i).end>=e) {
endFlag=false;
}else {
ns=intervals.get(i).start;
beginFlag=false;
}
}
}
if (endFlag&&!beginFlag) {
if (intervals.get(i).start>e) {
ne=e;
answerList.add(new Interval(ns,ne));
endFlag=false;
beginFlag=true;
}else if(intervals.get(i).start==e){
ne=intervals.get(i).end;
answerList.add(new Interval(ns,ne));
endFlag=false;
beginFlag=true;
continue;
}else if(intervals.get(i).end>=e){
ne=intervals.get(i).end;
answerList.add(new Interval(ns,ne));
endFlag=false;
beginFlag=true;
continue;
}else if(i==intervals.size()-1){
ne=e;
answerList.add(new Interval(ns,ne));
return answerList;
}
}
if (beginFlag) {
answerList.add(intervals.get(i));
}
}
if (endFlag) {
answerList.add(newInterval);
}
return answerList;
}
}