一. 堆栈的总结
二. Easy 题目
class CQueue {
private:
stack<int> stack1,stack2;
public:
CQueue() {
while(!stack1.empty()){
stack1.pop();
}
while(!stack2.empty()){
stack2.pop();
}
}
void appendTail(int value) {
stack1.push(value);
}
int deleteHead() {
int value =-1;
if(!stack2.empty()){
value=stack2.top();
stack2.pop();
}else{
while(!stack1.empty()){
stack2.push(stack1.top());
stack1.pop();
}
if(!stack2.empty()){
value=stack2.top();
stack2.pop();}
}
return value;
}
};
class MinStack {
private:
stack<int> stack1;
stack<int> min_s;
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
stack1.push(x);
if(min_s.empty()||x<= min_s.top() ){
min_s.push(x);
}
}
void pop() {
if(min_s.top() == stack1.top()){
min_s.pop();
}
stack1.pop();
}
int top() {
return stack1.top();
}
int min() {
return min_s.top();
}
};
网友评论