题解 | #数据流中的中位数#
数据流中的中位数
http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
public class Solution {
private ArrayList<Integer> data = new ArrayList<>();
public void Insert(Integer num) {
data.add(num);
}
public Double GetMedian() {
Collections.sort(data);
int length = data.size();
if(length % 2 != 0){
return (double)data.get(length / 2);
}else{
return (double)( data.get(length /2 - 1) +data.get(length / 2)) / 2;
}
}
}