- 用栈实现队列 232. Implement Queue using Stacks(Easy)
- 用队列实现栈 225. Implement Stack using Queues(Easy)
- 最小值栈 155. Min Stack(Easy)
- 用栈实现括号匹配 20. Valid Parentheses(Easy)
- 数组中元素与下一个比它大的元素之间的距离 739. Daily Temperatures(Medium)
- 循环数组中比当前元素大的下一个元素 503. Next Greater Element II(Medium)
栈和队列经典应用:括号匹配,BFS(队列)/DFS(栈),未完待续~
用栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> sk1, sk2;
MyQueue() {
/*
思路:用一个栈做中转,栈是先进后出,为了能够反序实现队列的先进先出,则另外用一个栈做中转
*/
}
/** Push element x to the back of queue. */
void push(int x) {
sk1.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(sk2.empty()){
while(!sk1.empty()){
sk2.push(sk1.top());
sk1.pop();
}
}
int result = sk2.top();
sk2.pop();
return result;
}
/** Get the front element. */
int peek() {
if(sk2.empty()){
while(!sk1.empty()){
sk2.push(sk1.top());
sk1.pop();
}
}
return sk2.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return sk1.empty() && sk2.empty();
}
};
用队列实现栈
class MyStack {
public:
/** Initialize your data structure here. */
queue<int> q;
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
for(int i = 0;i < q.size()-1; i++){
q.push(q.front());
q.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int x = q.front();
q.pop();
return x;
}
/** Get the top element. */
int top() {
return q.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q.empty();
}
};
最小值栈
class MinStack {
public:
/** initialize your data structure here. */
stack<int> sk;
stack<int> skmin;
MinStack() {
}
void push(int val) {
sk.push(val);
if((!skmin.empty() && val <= skmin.top()) || skmin.empty())
skmin.push(val);
}
void pop() {
int x = sk.top();
sk.pop();
if(x == skmin.top())
skmin.pop();
}
int top() {
return sk.top();
}
int getMin() {
return skmin.top();
}
};
用栈实现括号匹配
class Solution {
public:
bool isValid(string s) {
stack<char> sk;
for(int i = 0;i < s.size(); i++){
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
sk.push(s[i]);
if(s[i] == ')'){
if(!sk.empty() && sk.top() == '(')
sk.pop();
else
return false;
}
if(s[i] == ']'){
if(!sk.empty() && sk.top() == '[')
sk.pop();
else
return false;
}
if(s[i] == '}'){
if(!sk.empty() && sk.top() == '{')
sk.pop();
else
return false;
}
}
return sk.empty();
}
};
数组中元素与下一个比它大的元素之间的距离
typedef pair<int,int> P;
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& arr) {
/*
思路:用栈记录暂时没有找到比它大的值的元素,记录值和它所在的位置,每处理一个值都先检查下它比栈中哪些值大,更新这些值的结果然后压栈
时间复杂度:O(n)
空间复杂度:O(n)
*/
vector<int> ans(arr.size(), 0);
stack<P> sk;
for(int i = 0;i < arr.size(); i++){
while(!sk.empty() && arr[i] > sk.top().first){
P p = sk.top();
ans[p.second] = i - p.second;
sk.pop();
}
sk.push(make_pair(arr[i], i));
}
return ans;
}
};
循环数组中比当前元素大的下一个元素
typedef pair<int,int> P;
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& arr) {
/*
思路:用栈记录暂时没有找到比它大的值的元素,只记录它所在的位置,每处理一个值都先检查下它比栈中哪些值大,更新这些值的结果然后压栈,训练数列两次,则可得到循环数组的结果
时间复杂度:O(n)
空间复杂度:O(n)
*/
vector<int> ans(arr.size(), -1);
stack<int> sk;
for(int i = 0;i < arr.size()*2; i++){
int index = i % arr.size();
while(!sk.empty() && arr[index] > arr[sk.top()]){
ans[sk.top()] = arr[index];
sk.pop();
}
sk.push(index);
}
return ans;
}
};