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