栈、队列、堆类题目合集

  • 写在前面
  • 题目列表
  • 思路分析
  • 代码实现

写在前面

  1. 栈、队列、堆基本的题目就是这些,需要特殊技巧的另算,重写一遍保证熟练掌握这些题目并且达到熟练使用的程度。记录以便后续再次学习。

题目列表

    1. 用队列实现栈
    1. 用栈实现队列
    1. 最小栈
    1. 数组中的第K个最大元素
    1. 数据流的中位数

思路分析

    1. 用队列实现栈
    1. 用栈实现队列
    1. 最小栈
    1. 数组中的第K个最大元素(堆实现)
    1. 数据流的中位数(大小堆实现)
  1. 注意1-3题只是增加对栈和队列的数据结构的理解,而4-5主要学会使用堆,具体就是大小堆的理解和使用。这一点不管是用cpp还是java都是一样的。重在理解。
  2. 目前对于这两类的数据结构都掌握的更加清晰了。
  3. 继续保持!

代码实现

  1. 用队列实现栈
class MyStack {public:/** Initialize your data structure here. */MyStack() {}/** Push element x onto stack. */void push(int x) {// 新队伍queue<int> temp_queue;// 新来的先排temp_queue.push(x);// 老人再跟上while (!init_queue.empty()) {temp_queue.push(init_queue.front());init_queue.pop();}// 最后重新排回原始队列while (!temp_queue.empty()) {init_queue.push(temp_queue.front());temp_queue.pop();}}/** Removes the element on top of the stack and returns that element. */int pop() {int tmp = init_queue.front();init_queue.pop();return tmp;}/** Get the top element. */int top() {return init_queue.front();}/** Returns whether the stack is empty. */bool empty() {return init_queue.empty();}
private:queue<int> init_queue;
};/*** Your MyStack object will be instantiated and called as such:* MyStack* obj = new MyStack();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->top();* bool param_4 = obj->empty();*/
  1. 用栈实现队列
class MyQueue {public:/** Initialize your data structure here. */MyQueue() {}/** Push element x to the back of queue. */void push(int x) {// 临时栈stack<int> temp_stack;// 先将老人都出栈进新栈while (!init_stack.empty()) {temp_stack.push(init_stack.top());init_stack.pop();}// 再把新来的x入原来的栈init_stack,最后再把老人接回来init_stack.push(x);while (!temp_stack.empty()) {init_stack.push(temp_stack.top());temp_stack.pop();}}/** Removes the element from in front of queue and returns that element. */int pop() {int tmp =init_stack.top();init_stack.pop();return tmp;}/** Get the front element. */int peek() {return init_stack.top();}/** Returns whether the queue is empty. */bool empty() {return init_stack.empty();}
private:stack<int> init_stack;
};/*** Your MyQueue object will be instantiated and called as such:* MyQueue* obj = new MyQueue();* obj->push(x);* int param_2 = obj->pop();* int param_3 = obj->peek();* bool param_4 = obj->empty();*/
  1. 最小栈
class MinStack {public:/** initialize your data structure here. */MinStack() {}void push(int x) {// 将数据先压入数据栈_data,_data.push(x);if (_min.empty()) {_min.push(x);} else {if ( x > _min.top()) {x = _min.top();}_min.push(x);}}void pop() {_data.pop();_min.pop();}int top() {return _data.top();}int getMin() {return _min.top();}private:stack<int> _min;stack<int> _data;
};/*** Your MinStack object will be instantiated and called as such:* MinStack* obj = new MinStack();* obj->push(x);* obj->pop();* int param_3 = obj->top();* int param_4 = obj->getMin();*/
  1. 数组中的第K个最大元素
class Solution {public:int findKthLargest(vector<int>& nums, int k) {priority_queue<int,vector<int>,greater<int>> p_queue;for (int i = 0; i < nums.size(); ++i) {if (p_queue.size() < k) {p_queue.push(nums[i]); } else if (p_queue.top() < nums[i]) {p_queue.pop();p_queue.push(nums[i]);}}return p_queue.top();}
};
  1. 数据流的中位数
class MedianFinder {priority_queue<int> lo;                              // max heappriority_queue<int, vector<int>, greater<int>> hi;   // min heappublic:// Adds a number into the data structure.void addNum(int num){lo.push(num);                                    // Add to max heaphi.push(lo.top());                               // balancing steplo.pop();if (lo.size() < hi.size()) {                     // maintain size propertylo.push(hi.top());hi.pop();}}// Returns the median of current data streamdouble findMedian(){return lo.size() > hi.size() ? (double) lo.top() : (lo.top() + hi.top()) * 0.5;}
};/*** Your MedianFinder object will be instantiated and called as such:* MedianFinder* obj = new MedianFinder();* obj->addNum(num);* double param_2 = obj->findMedian();*/

20210310 20210311 :栈、队列、堆类题目合集相关推荐

  1. 20210308 20210309 :链表类题目合集

    链表类题目回顾 写在前面 题目 思路与算法 代码实现 写在前面 这一类的题目上一次编写是在刚好6个月之前,也就是我20200908和20200909的两篇博客涉及了今天这一篇的所有题目,均为链表相关. ...

  2. 牛客网-精华专题-前端校招面试题目合集

    前端校招面试题目合集 501 HTML CSS 前端基础 HTML 浏览器页面有哪三层构成,分别是什么,作用是什么? 构成:结构层(structural layer).表示层(presentation ...

  3. 20210325:力扣递归,回溯类型题目合集

    力扣递归,回溯类型题目合集 题目 思路与算法 代码实现 写在最后 题目 子集 2. 90. 子集 II 3. 40. 组合总和 II 4. 22. 括号生成 思路与算法 子集:注释的很详细,递归生成子 ...

  4. 高难度c语言编程题,高难度脑筋急转弯题目合集带答案

    脑筋急转弯,是指一些不能用惯性思维来回答的问题.经常玩这类游戏,可以锻炼人的发散思维以及应变能力,提高反应速度.今天给大家带来一些脑筋急转弯干货,希望可以帮助到有需要的同学! 高难度脑筋急转弯题目合集 ...

  5. 20210322 :贪心思想力扣典型题目合集

    贪心思想力扣典型题目合集 写在前面 题目列表 思路分析 代码实现 写在前面 贪心的思想很多时候在于想到那个贪心的点上,而对徒手书写某些代码结构的能力并不做要求,个人认为需要的是你敏锐的意识到这个贪心的 ...

  6. 中高级面试题题目合集

    题目合集持续更新ing- 前端基础github地址.README.md可以下载到typora中打开,会有整个大纲目录显示(github中markdown目录快捷生成方式不现实,之后可能会想办法生成贴过 ...

  7. 珍宝鸭的力扣练习(7):分治法题目合集

    动态规划和分治法的区别 动态规划也是一种分治思想(比如其状态转移方程就是一种分治),但与分治算法不同的是,分治算法是把原问题分解为若干个子问题,自顶向下求解子问题,合并子问题的解,从而得到原问题的解. ...

  8. 二叉树+链表+字符串+栈和队列高频面试题合集,已开源下载

    前言 为什么互联网资讯这么发达,但是没有出现技术人才井喷? 为什么会出现应届生薪资倒挂多年老员工的现象? 这个世界有太多的现象都可以用**"二八定律"**来解释. 20%拿着高工资 ...

  9. 二叉树+链表+字符串+栈和队列高频面试题合集

    前言 Spring 5 于 2017 年 9 月发布了通用版本 (GA),它标志着自 2013 年 12 月以来第一个主要 Spring Framework 版本.它提供了一些人们期待已久的改进,还采 ...

最新文章

  1. javascript之数组操作
  2. centos7 postgresql安装
  3. android手机微信收藏功能实现,Android模仿微信收藏文件的标签处理功能
  4. 在Mysql中count(*)、count(1)与count(字段/列名)的详解—聚合函数count
  5. SpringCloud集成lombox(eclipes工具)
  6. mysql索引需要了解的几个注意
  7. npm和gulp学习
  8. Go Elasticsearch 查询快速入门
  9. StyTr^2:Image Style Transfer with Transformers
  10. spark 安装详细步骤
  11. python二级题库(百分之九十原题) 刷题软件推荐
  12. sql-update 用法
  13. 哈希表、哈希桶(C++实现)
  14. Lab3 Report
  15. 署任DS.ENOVIA.DMU.NAVIGATOR.V5-6R2017.GA.WIN64数字制造解决方案
  16. 一个网站完整详细的SEO优化方案
  17. 求全排列(1) --- dfs 记录
  18. Excel 一些操作记录,方便自己查
  19. TP5 框架 SQL 执行流程分析及 5.0.9 SQL 注入漏洞分析
  20. ## **#安装数据库SQL Server 2008R2 时,提示安装或配置microsoft.net framework 3.5 sp1**

热门文章

  1. 吐血整理!顶级程序员的百宝箱来咯!| 原力计划
  2. 对抗弱网下的音视频难题,声网正式开源抗丢包音频编解码器 Agora SOLO!
  3. 人人在谈的物联网,入门开发真难!
  4. 5G来了,智能手机们还能拼什么?
  5. 阿里云智能 AIoT 首席科学家丁险峰:阿里全面进军 IoT 这一年 | 问底中国 IT 技术演进
  6. Linux curl 常用示例你都 Get 了吗?| CSDN 博文精选
  7. 移动开发或将被颠覆?
  8. 30 秒?!Chrome 插件带你速成编程学习 | 程序员硬核评测
  9. 影响 5000 万开发者,GitHub 与 CSDN 掌舵人对话技术社区未来
  10. 高德开放平台与360儿童手表达成合作,全球数据助力第三方企业