题解 | #数据流中的中位数#
数据流中的中位数
https://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
class Solution { public: void Insert(int num) { nums.push_back(num); } double GetMedian() { double res=0; sort(nums.begin(),nums.end()); int n=nums.size(); if(n%2){ res=nums[n/2]*1.0; } else{ if(n==2) return (nums[0]+nums[1])/2.0; res=nums[n/2-1]+nums[n/2]; res/=2.0; } return res; } private: vector<int> nums; };