题解 | #数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
import java.util.*;
public class Solution {
List<Double> list = new ArrayList<>();
public void Insert(Integer num) {
list.add(Double.valueOf(num));
}
public Double GetMedian() {
Collections.sort(list);
if (list.size() % 2 == 0) {
double n1 = list.get(list.size() / 2);
double n2 = list.get((list.size() - 1) / 2);
return (n1 + n2) / 2;
} else {
return list.get(list.size() / 2);
}
}
}
美的集团公司福利 873人发布