力扣155. 最小栈
我们只需要设计一个数据结构,使得每个元素 a 与其相应的最小值 m 时刻保持一一对应。因此我们可以使用一个辅助栈,与元素栈同步插入与删除,用于存储与每个元素对应的最小值。
class MinStack {
stack<int> x_stack;
stack<int> min_stack;
public:
MinStack() {
min_stack.push(INT_MAX);
}
void push(int x) {
x_stack.push(x);
min_stack.push(min(min_stack.top(), x));
}
void pop() {
x_stack.pop();
min_stack.pop();
}
int top() {
return x_stack.top();
}
int getMin() {
return min_stack.top();
}
};
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/min-stack/solution/zui-xiao-zhan-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
力扣232. 用栈实现队列
使用栈实现队列的下列操作:
push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。
class MyQueue {
public:
stack<int> stIn;
stack<int> stOut;
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
stIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
// 只有当stOut为空的时候,再从stIn里导入数据(导入stIn全部数据)
if (stOut.empty()) {
// 从stIn导入数据直到stIn为空
while(!stIn.empty()) {
stOut.push(stIn.top());
stIn.pop();
}
}
int result = stOut.top();
stOut.pop();
return result;
}
/** Get the front element. */
int peek() {
int res = this->pop(); // 直接使用已有的pop函数
stOut.push(res); // 因为pop函数弹出了元素res,所以再添加回去
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return stIn.empty() && stOut.empty();
}
};
作者:carlsun-2
链接:https://leetcode-cn.com/problems/implement-queue-using-stacks/solution/232-yong-zhan-shi-xian-dui-lie-liang-ge-zhan-lai-m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
力扣剑指 Offer 09. 用两个栈实现队列
class CQueue {
private:
stack<int> stack1, stack2;
public:
CQueue() {
}
void appendTail(int value) {
stack1.push(value);
}
int deleteHead() {
// 明确一点,题目要求queue弹出的元素,是stack1最下面的元素,也是stack2最上面的元素
int res = 0;
if(!stack2.empty())
{
// 如果stack2不是空的,那么其最上面的元素就是要被弹出的
res = stack2.top();
stack2.pop();
}
else if(!stack1.empty())
{
// 如果stack2已经空了,把stack1的元素压过去,然后弹出stack2最上面的元素
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
res = stack2.top();
stack2.pop();
}
else return -1; // 如果stack1和2都是空的,返回-1
return res;
}
};
/**
* Your CQueue object will be instantiated and called as such:
* CQueue* obj = new CQueue();
* obj->appendTail(value);
* int param_2 = obj->deleteHead();
*/
作者:superkakayong
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/solution/zi-jie-ti-ku-jian-9-jian-dan-yong-liang-ge-zhan-sh/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
力扣225. 用队列实现栈
使用队列实现栈的下列操作:
push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空
两个队列模拟:
class MyStack {
public:
queue<int> que1;
queue<int> que2; // 辅助队列
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
que1.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int size = que1.size();
size--;
while (size--) {
// 将que1 导入que2,但要留下最后一个元素
que2.push(que1.front());
que1.pop();
}
int result = que1.front(); // 留下的最后一个元素就是我们要返回的值
que1.pop();
que1 = que2; // 再将que2赋值给que1
while(!que2.empty()) {
// 清空que2
que2.pop();
}
return result;
}
/** Get the top element. */
int top() {
return que1.b
本文详细介绍了如何利用栈和队列解决力扣中的多个经典问题,包括最小栈、栈实现队列、两栈实现队列、队列实现栈、有效括号检查、逆波兰表达式求值、字符串解码、判断栈的压入弹出序列、基本计算器、每日温度的最大值计算、滑动窗口最大值和移除数字等。通过这些实例,展示了栈和队列在算法设计中的重要性和灵活性。
最低0.47元/天 解锁文章
3万+

被折叠的 条评论
为什么被折叠?



