栈和队列 | ||
1. 用栈实现队列 | 思考后可做出 | ※ |
2. 用队列实现栈 | 思考后可做出, 解法可简化 | ※ |
3. 最小值栈 | 思考后可做出 |
|
4. 用栈实现括号匹配 | 第一次可做出Ok |
|
5. 数组中元素与下一个比它大的元素之间的距离 | 思考后可做出, 解法可简化 | ※ |
6. 循环数组中比当前元素大的下一个元素 | 思考后可做出 | ※ |
1. 用栈实现队列
232. Implement Queue using Stacks (Easy)
https://leetcode-cn.com/problems/implement-queue-using-stacks/
栈的顺序为后进先出,而队列的顺序为先进先出。使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序。
class MyQueue {
private:
stack<int> st1,st2;
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
st1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
int val=peek();
st2.pop();
return val;
}
/** Get the front element. */
int peek() {
if(st2.empty())
while(!st1.empty()){
st2.push(st1.top());
st1.pop();
}
return st2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return st1.empty()&&st2.empty();
}
};
/**
* 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();
*/
2. 用队列实现栈
225. Implement Stack using Queues (Easy)
https://leetcode-cn.com/problems/implement-stack-using-queues/
在将一个元素 x 插入队列时,为了维护原来的后进先出顺序,需要让 x 插入队列首部。而队列的默认插入顺序是队列尾部,因此在将 x 插入队列尾部之后,需要让除了 x 之外的所有元素出队列,再入队列。
一个队列即可实现!!!
class MyStack {
private:
queue<int> q;
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i=q.size();i>1;i--){
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int val=q.front();
q.pop();
return val;
}
/** Get the top element. */
int top() {
return q.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
};
/**
* 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();
*/
3. 最小值栈
155. Min Stack (Easy)
https://leetcode-cn.com/problems/min-stack/
可用两个栈实现
4. 用栈实现括号匹配
20. Valid Parentheses (Easy)
https://leetcode-cn.com/problems/valid-parentheses/
5. 数组中元素与下一个比它大的元素之间的距离
739. Daily Temperatures (Medium)
https://leetcode-cn.com/problems/daily-temperatures/
在遍历数组时用栈把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
vector<int> ret(T.size());
stack<int> maxT;
for(int i=T.size()-1;i>=0;i--){
while(!maxT.empty()&&T[i]>=T[maxT.top()])
maxT.pop();
ret[i]=(maxT.empty())?0:(maxT.top()-i);
maxT.push(i);
}
return ret;
}
};
6. 循环数组中比当前元素大的下一个元素
503. Next Greater Element II (Medium)
https://leetcode-cn.com/problems/next-greater-element-ii/
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
stack<int> mst;
vector<int> ret(nums.size());
for(int i=nums.size()-2;i>=0;i--) mst.push(nums[i]);
for(int i=nums.size()-1;i>=0;i--){
while(!mst.empty()&&nums[i]>=mst.top())
mst.pop();
ret[i]=mst.empty()?-1:mst.top();
mst.push(nums[i]);
}
return ret;
}
};