泰6996三合一片,三合一吸尘扫地机
墨初 知识笔记 54阅读
题目来源
leetcode题目网址面试题 03.01. 三合一 - 力扣LeetCode

解题思路
新建一个长度为 3*stackSize 的数组每个stackSize 存放一个栈0stackSize2*stackSize 分别为三个栈的栈底之后模拟栈的操作即可。

解题代码
class TripleInOne {private: int top1; int top2; int top3; vector<int> stack; int stackSize;public: TripleInOne(int stackSize) { vector<int> thisStack(stackSize*3); this->stackSizestackSize; stackthisStack; top10; top2stackSize; top32*stackSize; } void push(int stackNum, int value) { stackNum; if(stackNum1){ if(top1stackSize){ return ; } stack[top1]value; top1; }else if(stackNum2){ if(top22*stackSize){ return ; } stack[top2]value; top2; }else{ if(top33*stackSize){ return ; } stack[top3]value; top3; } } int pop(int stackNum) { stackNum; if(stackNum1){ return top10?-1:stack[--top1]; }else if(stackNum2){ return top2stackSize?-1:stack[--top2]; }else{ return top32*stackSize?-1:stack[--top3]; } } int peek(int stackNum) { stackNum; if(stackNum1){ return top10?-1:stack[top1-1]; }else if(stackNum2){ return top2stackSize?-1:stack[top2-1]; }else{ return top32*stackSize?-1:stack[top3-1]; } } bool isEmpty(int stackNum) { stackNum; if(stackNum1){ return top10?true:false; }else if(stackNum2){ return top2stackSize?true:false; }else{ return top32*stackSize?true: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); */
总结
刚开始以为是双向队列结果是栈。
无官方题解。
标签: