c++

数据流中的中位数

http://www.nowcoder.com/questionTerminal/9be0172896bd43948f8a32fb954e1be1

class Solution {
public:
    void Insert(int num)
    {
        int cnt = heap1.size()+heap2.size();
        if (cnt&1) {
            heap2.push(num);
            heap1.push(heap2.top());
            heap2.pop();
        } else {
            heap1.push(num);
            heap2.push(heap1.top());
            heap1.pop();
        }
    }

    double GetMedian()
    { 
        int cnt = heap1.size()+heap2.size();
        if (cnt&1) return heap2.top();
        else return (heap1.top()+heap2.top())/2.0;
    }
private:
    priority_queue<int,vector<int>,greater<int> > heap1;
    priority_queue<int,vector<int>,less<int> > heap2;
};
全部评论

相关推荐

嵌入式的小白:面试少的,说明你的投递的岗位和简历匹配度不高,技术这个东西很杂的,你这种情况,建议 1.看看嵌入式招聘的岗位需求,会有不同大方向的,比如MCU,RTOS的,或者linux上驱动的,或者应用层的,这都是简单分类,但对技术要求差异很大的 2.结合你的经验,看能和哪类匹配上,就找对应类别的 3.简历和招聘岗位需求对着看下,看人家需要啥,你会啥,匹配度高才有会高概率有面试的
秋招的第一个offer,...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务