forked from Blankj/awesome-java-leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0295
41 lines (31 loc) · 802 Bytes
/
0295
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
class MedianFinder {
List<Double> lst ;
public MedianFinder() {
lst = new ArrayList<>();
}
public void addNum(int num) {
double n = num*1.0;
/* for(int i=0;i<lst.size();i++)
{
if(lst.get(i)>n)
{
lst.add(i,n);
return;
}
} */
lst.add(n);
}
public double findMedian() {
Collections.sort(lst);
int sz= lst.size();
//System.out.println(lst.size());
if(sz%2!=0)
{
return lst.get(sz/2);
}
else
{
return (lst.get(sz/2)+lst.get((sz-1)/2))/2.0;
}
}
}