程序员面试金典——18.9实时中位数

Solution1:我的答案。利用排序,比较弱智。。

class Middle {
public:vector<int> getMiddle(vector<int> A, int n) {// write code herevector<int> res;if (n <= 0)return res;auto iter1 = A.begin(), iter2 = ++A.begin();for (; iter2 <= A.end(); iter2++) {sort(iter1, iter2);res.push_back(A[(iter2 - iter1 - 1) / 2]);//注意准确计算下标}return res;}
};

Solution2:利用优先队列priority_queue,说实话还是第一次用呢~~
此题思路如下图所示:

利用priority_queue自动排序了
关于priority_queue介绍的博客:
[1]https://blog.csdn.net/pzhu_cg_csdn/article/details/79166858
[2]http://www.cplusplus.com/reference/queue/priority_queue/

class Middle {
public:vector<int> getMiddle(vector<int> A, int n) {// write code herepriority_queue<int, vector<int>, less<int> > small;priority_queue<int, vector<int>, greater<int> > big;vector<int> ans;for (int i = 0; i < A.size(); ++i) {if (small.empty() || A[i] <= small.top())small.push(A[i]);elsebig.push(A[i]);if (small.size() == big.size() + 2) {big.push(small.top());small.pop();}else if (small.size() == big.size() - 1) {small.push(big.top());big.pop();}ans.push_back(small.top());}return ans;}
};

Solution3:
利用最大堆和最小堆实现,其思想和用priority_queue差不多,但是代码却麻烦了好多。。。。还是优先队列好啊
关于最大堆和最小堆的介绍:
[1]https://blog.csdn.net/guoweimelon/article/details/50904346
关于push_heap和pop_heap的介绍
[1]https://blog.csdn.net/fuyufjh/article/details/47144869

class Middle {
public:vector<int> getMiddle(vector<int> A, int n) {// write code herevector<int> maxheap;vector<int> minheap;vector<int> re;for (int i = 0; i < n; ++i) {if (maxheap.size() == minheap.size()) {if (minheap.size() > 0 && minheap.front() < A[i]) {pop_heap(minheap.begin(), minheap.end(), greater<int>());maxheap.push_back(minheap.back());push_heap(maxheap.begin(), maxheap.end(), less<int>());minheap.pop_back();minheap.push_back(A[i]);push_heap(minheap.begin(), minheap.end(), greater<int>());}else {maxheap.push_back(A[i]);push_heap(maxheap.begin(), maxheap.end(), less<int>());}}else {if(maxheap.front() > A[i]) {pop_heap(maxheap.begin(), maxheap.end(), less<int>());minheap.push_back(maxheap.back());push_heap(minheap.begin(), minheap.end(), greater<int>());maxheap.pop_back();maxheap.push_back(A[i]);push_heap(maxheap.begin(), maxheap.end(), less<int>());}else {minheap.push_back(A[i]);push_heap(minheap.begin(), minheap.end(), greater<int>());}}re.push_back(maxheap.front());}return re;}
};

程序员面试金典——18.9实时中位数相关推荐

  1. 程序员面试金典——18.13 最大字母矩阵

    程序员面试金典--18.13 最大字母矩阵 在牛客网上把此题的难度给大大降低了......... Solution1: 参考网址:https://www.nowcoder.com/questionTe ...

  2. 程序员面试金典——18.12最大和子矩阵

    程序员面试金典--18.12最大和子矩阵 Solution1: 参考网址: [1]https://www.cnblogs.com/GodA/p/5237061.html 思想讲的很清楚~ [2]htt ...

  3. 【To Do】程序员面试金典——18.11最大子方阵

    程序员面试金典--18.11最大子方阵 Solution1:我的答案.最笨的方法,时间复杂度是O(n3)O(n3)O(n^3) class SubMatrix { public:int maxSubM ...

  4. 程序员面试金典——18.10字符串变换

    程序员面试金典--18.10字符串变换 Solution1: 我的答案.穷举法,个人认为此题还是有点难度的... 利用了倒推法以及很高的时间复杂度才解决,并不值得推崇呀. class Change { ...

  5. 程序员面试金典——18.7最长合成字符串

    程序员面试金典--18.7最长合成字符串 参考网址:https://www.nowcoder.com/profile/2896594/codeBookDetail?submissionId=13543 ...

  6. 【To Do!】程序员面试金典——18.8子串判断

    程序员面试金典--18.8子串判断 Solution1:我的答案 利用了C++ STL中自带的find函数,有点投机取巧的意思,正统方法是用trie树(单词查找树)来做,那就麻烦了许多 class S ...

  7. 程序员面试金典——18.4 2的个数

    程序员面试金典--18.4 2的个数 Solution1:经典通法,得牢记啊... 此题在<剑指offer>中出现过,里面分析的比较到位 https://blog.csdn.net/all ...

  8. 程序员面试金典——18.5单词最近的距离

    程序员面试金典--18.5单词最近的距离 Solution1:我的答案,时间复杂度为O(n2)O(n2)O(n^2). class Distance { public:int getDistance( ...

  9. 程序员面试金典——18.1另类加法

    程序员面试金典--18.1另类加法 Solution1:还是参考剑指上的思路.. class UnusualAdd {public:int addAB(int A, int B) {// write ...

最新文章

  1. kobject_create_and_add
  2. BZOJ3534:[SDOI2014]重建(矩阵树定理)
  3. html 属性中嵌套php,如何在PHP中使用嵌套数组创建HTML数据属性字符串?
  4. JDK9新特性实战:简化流关闭新姿势
  5. nginx: [error] open() “/usr/local/var/run/nginx.pid“ failed (2: No such file or directory)
  6. 直播连麦贾扬清,谈谈他所理解的四大 AI 落地问题 | 攻“疫”技术公开课
  7. OpenCV 4.5 发布!
  8. python unittest断言_python接口自动化(二十四)--unittest断言——中(详解)
  9. [转]上海新东方vs新东方,SEO实战
  10. micropython文件上传软件_4-3 为NodeMCU刷入MicroPython固件
  11. 2021年“泰迪杯”数据分析技能赛A题
  12. 王道计算机考研图书勘误表公布!
  13. VTK读取rawdata
  14. 在网易游戏的第三年——Jerish的2021总结
  15. centos7下安装mysql5.7(rpm)
  16. m4a转wav格式方法和步骤
  17. win10 凭据管理
  18. 【蓝桥杯程序设计大赛感想】 一路艰辛 一路收获
  19. 信号的用法,signal函数、sigaction函数及信号集(sigemptyset,sigaddset)操作函数
  20. linux 查看es进程,Linux---关闭Elasticsearch进程,并重新启动

热门文章

  1. 数据结构上机实践第二周项目2- 程序的多文件组织
  2. 【华为OJ】按单词将句子逆序
  3. 【字符串】面试题之奇偶字符串分离
  4. 比较好的浏览器_一款安卓黑科技手机浏览器 体积很小,功能很6!
  5. 计算机的进制数思想在哪方面有应用,计算机应用技术问答题(一)
  6. oracle 包含的对象,oracle – 我可以创建一个包含嵌套表作为属性的对象表吗?
  7. python将对象放入列表_将所有python-rom对象放入列表
  8. python画数学函数_Python 绘制你想要的数学函数图形 !
  9. 多台Linux服务器一起关机,linux – 一台服务器,两台APC UPS上的冗余电源:如何触发关机?...
  10. scala设计模式_Scala中的工厂设计模式