-
Notifications
You must be signed in to change notification settings - Fork 2
/
InsertInterval.java
66 lines (41 loc) · 959 Bytes
/
InsertInterval.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
package com.c15;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class InsertInterval {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static List<int[]> insertInterval(List<int[]> list,int[] m){
List<int[]> newList = new ArrayList<int[]>();
if (list==null){
if (m != null){
newList.add(m);
}
return newList;
}
if (m==null)
return newList;
for(int i=0;i<list.size();i++){
if (m[0] <= list.get(i)[0]){
list.add(i, m);
}
}
LinkedList<int[]> link = new LinkedList<int[]>(list);
while(link.size()>1){
int[] p1 = link.pop();
int[] p2 = link.pop();
if (p1[1] >= p2[0]){
link.push(new int[]{p1[0],Math.max(p1[1],p2[1])});
}else{
link.push(p2);
newList.add(p1);
}
}
newList.addAll(link);
return newList;
}
}