这道题看半天很懵,不知道在讲什么,后来通过示例发现,原来所谓数据流是插入和返回中位数是结合的产物。
import java.util.*;
public class Solution {
ArrayList<Integer> list = new ArrayList<>();
public void Insert(Integer num) {
list.add(num);
}
public Double GetMedian() {
Collections.sort(list);
int length = list.size();
// 求中位数
if(length % 2!=0){
return (double)list.get(length/2);
}else{
return (double)(list.get(length/2-1) + list.get(length/2))/2;
}
}
}