题解 | #数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
import java.util.*; public class Solution { private ArrayList<Integer> val = new ArrayList<Integer>(); public void Insert(Integer num) { if (val.isEmpty()) val.add(num); else { int i = 0; for (; i < val.size(); i++) { if (num <= val.get(i)){ break; } } val.add(i, num); } } public Double GetMedian() { int n = val.size(); if (n % 2 == 1) return (double)val.get(n / 2); else { double a = val.get(n / 2); double b = val.get(n / 2 - 1); return (a + b) / 2; } } }