参考:https://github.com/CyC2018/CS-Notes
1. 用栈实现队列
232. Implement Queue using Stacks (Easy)
栈的顺序为后进先出,而队列的顺序为先进先出。使用两个栈实现队列,一个元素需要经过两个栈才能出队列,在经过第一个栈时元素顺序被反转,经过第二个栈时再次被反转,此时就是先进先出顺序。
class MyQueue {
public:
/** Initialize your data structure here. */
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
_new.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
shiftStack();
int val = _old.top(); _old.pop();
return val;
}
/** Get the front element. */
int peek() {
shiftStack();
return _old.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return _old.empty() && _new.empty();
}
void shiftStack(){
if(!_old.empty()) return;
while(!_new.empty()){
_old.push(_new.top());
_new.pop();
}
}
private:
stack<int> _old, _new;
};
/**
* 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)
在将一个元素 x 插入队列时,为了维护原来的后进先出顺序,需要让 x 插入队列首部。而队列的默认插入顺序是队列尾部,因此在将 x 插入队列尾部之后,需要让除了 x 之外的所有元素出队列,再入队列。
class MyStack {
public:
/** Initialize your data structure here. */
MyStack() {
}
/** Push element x onto stack. */
void push(int x) {
q2.push(x);
while(!q2.empty()){
q1.push(q2.front());
q2.pop();
}
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
int x = top();
q2.pop();
return x;
}
/** Get the top element. */
int top() {
if(q2.empty()){
for(int i=0; i<q1.size()-1; ++i){
q1.push(q1.front());
q1.pop();
}
q2.push(q1.front());
q1.pop();
}
return q2.front();
}
/** Returns whether the stack is empty. */
bool empty() {
return q1.empty() && q2.empty();
}
private:
queue<int> q1, q2;
};
/**
* 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)
class MinStack {
public:
/** initialize your data structure here. */
stack<int> mins;
stack<int> s;
MinStack() {
}
void push(int x) {
s.push(x);
if(mins.empty() || mins.top() >=x){
mins.push(x);
}
}
void pop() {
if(s.top() == mins.top())
mins.pop();
s.pop();
}
int top() {
return s.top();
}
int min() {
return mins.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->min();
*/
对于实现最小值队列问题,可以先将队列使用栈来实现,然后就将问题转换为最小值栈,这个问题出现在 编程之美:3.7。
4. 用栈实现括号匹配
20. Valid Parentheses (Easy)
"()[]{}"
Output : true
class Solution {
public:
bool isValid(string s) {
stack<char> parentheses;
for(auto c:s){
if(c =='(' || c=='{' || c=='[') parentheses.push(c);
else{
if(parentheses.empty()) return false;
if(c==')' && parentheses.top() !='(') return false;
if(c==']' && parentheses.top() !='[') return false;
if(c=='}' && parentheses.top() !='{') return false;
parentheses.pop();
}
}
return parentheses.empty();
}
};
5. 数组中元素与下一个比它大的元素之间的距离
739. Daily Temperatures (Medium)
Input: [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]
在遍历数组时用栈把数组中的数存起来,如果当前遍历的数比栈顶元素来的大,说明栈顶元素的下一个比它大的数就是当前元素。
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& T) {
int n = T.size();
vector<int> res(n, 0);
stack<int> s;
for(int i=0; i<n; ++i){
while(!s.empty() && T[i]>T[s.top()]){
auto t = s.top(); s.pop();
res[t] = i - t;
}
s.push(i);
}
return res;
}
};
6. 循环数组中比当前元素大的下一个元素
503. Next Greater Element II (Medium)
Input: [1,2,1]
Output: [2,-1,2]
Explanation: The first 1's next greater number is 2;
The number 2 can't find next greater number;
The second 1's next greater number needs to search circularly, which is also 2.
与 739. Daily Temperatures (Medium) 不同的是,数组是循环数组,并且最后要求的不是距离而是下一个元素。
class Solution {
public:
vector<int> nextGreaterElements(vector<int>& nums) {
int n = nums.size();
vector<int> res(n, -1);
stack<int> s;
for(int i=0; i<2*n; ++i){
int num = nums[i%n];
while(!s.empty() && num>nums[s.top()]){
res[s.top()] = num;
s.pop();
}
if(i<n)
s.push(i);
}
return res;
}
};