题解 | #c++ 巧妙的double转字符串 不知道你们怎么做的#
数据流中的中位数
http://www.nowcoder.com/practice/9be0172896bd43948f8a32fb954e1be1
class Solution {
private:
vector<double> nums;//记录当前已经插入数据
double mid;//记录中位数的位置
public:
Solution():mid(-0.5){
}
void Insert(int num) {
nums.push_back((double)num);
mid+=0.5;sort(nums.begin(),nums.end());
}
double GetMedian() {
double midnum;
string str = to_string(mid);
char ch = str[str.find_first_of('.')+1];
if(ch == '5'){
midnum = (nums[(int)(mid-0.5)]+nums[(int)(mid+0.5)])/2;
}else{
midnum = nums[mid];
}
return midnum;
}
};