栈、队列和堆
1、用栈实现队列
class MyQueue { stack<int> stack1,stack2; public: /** Initialize your data structure here. */ MyQueue() { while(!stack1.empty()) stack1.pop(); while(!stack2.empty()) stack2.pop(); } /** Push element x to the back of queue. */ void push(int x) { stack1.push(x); } /** Removes the element from in front of queue and returns that element. */ int pop() { if (stack2.empty()) { while(!stack1.empty()) { int val = stack1.top(); stack1.pop(); stack2.push(val); } } if(stack2.empty()) return -1; else { int val = stack2.top(); stack2.pop(); return val; } } /** Get the front element. */ int peek() { if (stack2.empty()) { while(!stack1.empty()) { int val = stack1.top(); stack1.pop(); stack2.push(val); } } if(stack2.empty()) return -1; return stack2.top(); } /** Returns whether the queue is empty. */ bool empty() { return stack1.empty() && stack2.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、用队列实现栈
class MyStack { queue<int> queue1; //主队列 queue<int> queue2; //辅助队列 public: /** Initialize your data structure here. */ MyStack() { while(!queue1.empty()) queue1.pop(); while(!queue2.empty()) queue2.pop(); } /** Push element x onto stack. */ void push(int x) { queue1.push(x); } /** Removes the element on top of the stack and returns that element. */ int pop() { if (queue1.empty()) return -1; if(queue2.empty()) { while(queue1.size()!=1) { int val = queue1.front(); queue1.pop(); queue2.push(val); } } int temp = queue1.front(); queue1.pop(); while(!queue2.empty()) { int val = queue2.front(); queue2.pop(); queue1.push(val); } return temp; } /** Get the top element. */ int top() { return queue1.back(); } /** Returns whether the stack is empty. */ bool empty() { return queue1.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、最小栈
方法:使用辅助栈
class MinStack { stack<int>data_stack; stack<int>min_stack; public: /** initialize your data structure here. */ MinStack() { while(!data_stack.empty()) data_stack.pop(); while(!min_stack.empty()) min_stack.pop(); } void push(int x) { data_stack.push(x); if (min_stack.empty()) min_stack.push(x); else{ if (min_stack.top() > x) { min_stack.push(x); } else min_stack.push(min_stack.top()); } } void pop() { if (data_stack.empty()) return; data_stack.pop(); min_stack.pop(); } int top() { return data_stack.top(); } int getMin() { return min_stack.top(); } }; /** * 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(); */
4、验证栈序列
方法:考察堆栈,那么就用堆栈模拟
先按 pushed 数组入栈,当入栈元素等于 popped 数组对应的数字之后,把该元素出栈,最后如果正确的话栈肯定是空的
class Solution { public: bool validateStackSequences(vector<int>& pushed, vector<int>& popped) { stack<int> s; int j=0;//popped的index for(int i=0;i<pushed.size();++i) { s.push(pushed[i]); while(!s.empty()&&s.top()==popped[j]) { s.pop(); j++; } } while(!s.empty()&&j<popped.size()) { if(s.top()==popped[j]) { s.pop(); j++; } else { return false; } } return true; } };