C++ LeetCode 栈 队列 堆 专题
2020-04-27 07:18
766 查看
文章目录
前提知识点
1. stack
栈,先进后出的线性表,可理解为一口水井。
常用函数:
S.top() 取出栈顶
S.empty() 判断栈是否为空
S.push(x) 将x添加至栈
S.pop() 弹出栈顶
S.size() 栈的存储元素个数
2. queue
队列,先进先出的线性表,可理解为一根水平管道。
常用函数
Q.empty() 判断队列是否为空
Q.front() 返回队列头部元素
Q.back() 返回队列尾部元素
Q.pop() 弹出队列头部元素
Q.push(x) 将x添加至队列尾部
Q.size() 返回队列存储元素的个数
3.priority_queue
STL优先级队列(二叉堆),最大(最小)值先出的完全二叉树。
#include<queue> std::priority_queue<int>big_heap;//默认构造最大堆 std::priority_queue<int,std::vector<int>, std::greater<int> >small_heap;//最小堆构造方法 std::priority_queue<int,std::vector<int>, std::less<int> >big_heap2;//最大堆构造方法
常用函数
big_heap.empty() 判断堆是否为空
big_heap.pop() 弹出堆顶元素(最大值)
big_heap.push(x) 将元素x添加至二叉堆
big_heap.top() 返回堆顶元素(最大值)
big_heap.size() 返回堆中元素个数
LeetCode 225. Implement Stack using Queues
难度:Easy
使用临时队列,注意题目中queue为复数!
class MyStack { public: /** Initialize your data structure here. */ MyStack() { } /** Push element x onto stack. */ void push(int x) { std::queue<int>temp_queue; temp_queue.push(x); while(!data_queue.empty()){ temp_queue.push(data_queue.front()); data_queue.pop(); } while(!temp_queue.empty()){ data_queue.push(temp_queue.front()); temp_queue.pop(); } } /** Removes the element on top of the stack and returns that element. */ int pop() { int a=data_queue.front(); data_queue.pop(); return a; } /** Get the top element. */ int top() { return data_queue.front(); } /** Returns whether the stack is empty. */ bool empty() { return data_queue.empty(); } private: std::queue<int>data_queue; };
LeetCode 232. Implement Queue using Stacks
难度:Easy
方法一:使用双栈,题目中stack为复数!
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { data_stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { if(data_stack2.empty()){ while(!data_stack1.empty()){ data_stack2.push(data_stack1.top()); data_stack1.pop(); } } int a=data_stack2.top(); data_stack2.pop(); return a; } /** Get the front element. */ int peek() { if(data_stack2.empty()){ while(!data_stack1.empty()){ data_stack2.push(data_stack1.top()); data_stack1.pop(); } } return data_stack2.top(); } /** Returns whether the queue is empty. */ bool empty() { return data_stack1.empty()&&data_stack2.empty(); } private: std::stack<int>data_stack1; std::stack<int>data_stack2; };
方法二:使用临时栈
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x to the back of queue. */ void push(int x) { std::stack<int>temp_stack; while(!data_stack1.empty()){ temp_stack.push(data_stack1.top()); data_stack1.pop(); } temp_stack.push(x); while(!temp_stack.empty()){ data_stack1.push(temp_stack.top()); temp_stack.pop(); } } /** Removes the element from in front of queue and returns that element. */ int pop() { int a=data_stack1.top(); data_stack1.pop(); return a; } /** Get the front element. */ int peek() { return data_stack1.top(); } /** Returns whether the queue is empty. */ bool empty() { return data_stack1.empty(); } private: std::stack<int>data_stack1; };
LeetCode 155. Min Stack
难度:Easy
class MinStack { public: /** initialize your data structure here. */ MinStack() { } void push(int x) { if(data_stack.empty()){ data_stack.push(x); min_stack.push(x); } else{ if(x<min_stack.top()){ min_stack.push(x); } else{ min_stack.push(min_stack.top()); } data_stack.push(x); } } void pop() { data_stack.pop(); min_stack.pop(); } int top() { return data_stack.top(); } int getMin() { return min_stack.top(); } private: std::stack<int>data_stack; std::stack<int>min_stack; };
poj 1363 Rails
难度:Medium
bool check_is_valid_order(std::queue<int>&order){ std::stack<int>temp_stack; int n=order.size(); for(int i=1;i<=n;i++){ temp_stack.push(i); while(!temp_stack.empty()&&temp_stack.top()==order.front()){ temp_stack.pop(); order.pop(); } } if(!temp_stack.empty()){ return false; } return true; }
LeetCode 215. Kth Largest Element in an Array
难度:Easy
方法一:
class Solution { public: int findKthLargest(vector<int>& nums, int k) { int size_nums=nums.size(); std::sort(nums.begin(),nums.end()); return nums[size_nums-k]; } };
方法二:
class Solution { public: int findKthLargest(vector<int>& nums, int k) { std::priority_queue<int>big_heap; for(int i=0;i<nums.size();i++){ big_heap.push(nums[i]); } for(int i=0;i<k-1;i++){ big_heap.pop(); } return big_heap.top(); } };
方法三:最优解
class Solution { public: int findKthLargest(vector<int>& nums, int k) { std::priority_queue<int, std::vector<int>, std::greater<int> >small_heap; for(int i=0;i<nums.size();i++){ if(small_heap.size()<k){ small_heap.push(nums[i]); } else { if(nums[i]>small_heap.top()){ small_heap.pop(); small_heap.push(nums[i]); } } } return small_heap.top(); } };
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- LeetCode(六)二分与单调队列_栈专题
- leetcode刷题循环队列 (C++)
- C++STL专题-队列(queue)、动态数组(vector)
- leetcode1548题:面试题59 - II. 队列的最大值(2020/3/7/每日一题)(python3和C++解法)
- 【搜索入门专题1】 hdu1242 J - Rescue c++ stl容器之优先队列+BFS
- C++使用队列实现栈(LeetCode 225)
- C++ LeetCode 贪心算法专题
- LeetCode 617. Merge Two Binary Trees (C++)
- leetcode寻找两个有序数组的中位数C++版
- [leetcode]用栈实现队列[javascript]
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
- 【LeetCode】120. Triangle 基于C++和Java的分析及解法,动态规划
- 队列的入队,出队,测长度,打印c++代码
- LeetCode之Search in Rotated Sorted Array II ()C++
- [C++]LeetCode 5: Longest Palindromic Substring(最长回文子串)
- leetcode_c++:链表:Linked List Cycle II(142)
- 【LeetCode-24】Swap Nodes in Pairs(C++)
- 【C++】 LeetCode 32. Longest Valid Parentheses
- 数据结构之队列的基本操作入队出队初始化删除-c++代码实现及运行实例结果
- Leetcode 225,232 用两个队列实现栈,用两个栈实现队列