写在前面
- 栈、队列、堆基本的题目就是这些,需要特殊技巧的另算,重写一遍保证熟练掌握这些题目并且达到熟练使用的程度。记录以便后续再次学习。
题目列表
-
- 用队列实现栈
- 用队列实现栈
-
- 用栈实现队列
- 用栈实现队列
-
- 最小栈
- 最小栈
-
- 数组中的第K个最大元素
- 数组中的第K个最大元素
-
- 数据流的中位数
- 数据流的中位数
思路分析
-
- 用队列实现栈
-
- 用栈实现队列
-
- 最小栈
-
- 数组中的第K个最大元素(堆实现)
-
- 数据流的中位数(大小堆实现)
- 注意1-3题只是增加对栈和队列的数据结构的理解,而4-5主要学会使用堆,具体就是大小堆的理解和使用。这一点不管是用cpp还是java都是一样的。重在理解。
- 目前对于这两类的数据结构都掌握的更加清晰了。
- 继续保持!
代码实现
-
- 用队列实现栈
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();
*/
-
- 用栈实现队列
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();
*/
-
- 最小栈
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();
*/
-
- 数组中的第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();
}
};
-
- 数据流的中位数
class MedianFinder {
priority_queue<int> lo; // max heap
priority_queue<int, vector<int>, greater<int>> hi; // min heap
public:
// Adds a number into the data structure.
void addNum(int num)
{
lo.push(num); // Add to max heap
hi.push(lo.top()); // balancing step
lo.pop();
if (lo.size() < hi.size()) {
// maintain size property
lo.push(hi.top());
hi.pop();
}
}
// Returns the median of current data stream
double 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();
*/