网友热评:说唱学院出的题
整个题目就第一句有用,示例为我们提出,有三个stack,分别为stack0.stack1,stack2。
所以这个简单题就是一个函数三个步骤呗。
复习了一下stack的库函数,top(),size(),empty();
看起来很容易其实记多了容易混淆,一些细节会失真
写了不过几分钟,调了十几分钟,草。
class TripleInOne {
public:
stack<int>stack0;
stack<int>stack1;
stack<int>stack2;
int maxsize;
TripleInOne(int stackSize) {
maxsize=stackSize;
}
void push(int stackNum, int value) {
if(stackNum==1&&stack1.size()<maxsize)stack1.push(value);
if(stackNum==2&&stack2.size()<maxsize)stack2.push(value);
if(stackNum==0&&stack0.size()<maxsize)stack0.push(value);
}
int pop(int stackNum) {
int j=-1;
if(stackNum==1&&!stack1.empty()){
j=stack1.top();
stack1.pop();}
if(stackNum==2&&!stack2.empty()){j=stack2.top();stack2.pop();}
if(stackNum==0&&!stack0.empty()){j=stack0.top();stack0.pop();}
return j;
}
int peek(int stackNum) {
if(stackNum==1&&!stack1.empty())return stack1.top();
if(stackNum==2&&!stack2.empty())return stack2.top();
if(stackNum==0&&!stack0.empty())return stack0.top();
return -1;
}
bool isEmpty(int stackNum) {
if(stackNum==1&&stack1.empty())return true;
if(stackNum==2&&stack2.empty())return true;
if(stackNum==0&&stack0.empty())return true;
return false;
}
};
/**
* Your TripleInOne object will be instantiated and called as such:
* TripleInOne* obj = new TripleInOne(stackSize);
* obj->push(stackNum,value);
* int param_2 = obj->pop(stackNum);
* int param_3 = obj->peek(stackNum);
* bool param_4 = obj->isEmpty(stackNum);
*/