三合一。描述如何只用一个数组来实现三个栈。
你应该实现push(stackNum, value)、pop(stackNum)、isEmpty(stackNum)、peek(stackNum)方法。stackNum表示栈下标,value表示压入的值。
构造函数会传入一个stackSize参数,代表每个栈的大小。
示例1:
输入:
["TripleInOne", "push", "push", "pop", "pop", "pop", "isEmpty"]
[[1], [0, 1], [0, 2], [0], [0], [0], [0]]
输出:
[null, null, null, 1, -1, -1, true]
说明:当栈为空时`pop, peek`返回-1,当栈满时`push`不压入元素。
分析: 本题要求在一个数组上实现三个站栈,所以我们在代码实现时可以这样考虑,一个栈的时候对应的操作是什么? 然后主要的不同是我们要对应栈的起始点,根据对应的公式进行换算:
class TripleInOne {
public:
int *stack;
int top[3];
int stackSize;
TripleInOne(int stackSize):stackSize(stackSize) {
stack = new int[stackSize*3];
top[0]=top[1]=top[2]=0; //定义每个栈的标志位;
}
void push(int stackNum, int value) {
if(top[stackNum] < stackSize)
stack[stackSize*stackNum + top[stackNum]++]=value;
}
int pop(int stackNum) {
if(top[stackNum] <= 0)
return -1;
else
return stack[stackSize*stackNum + (--top[stackNum])];
}
int peek(int stackNum) {
if(top[stackNum] <= 0)
return -1;
else
return stack[stackSize*stackNum + (top[stackNum]-1)];
}
bool isEmpty(int stackNum) {
return top[stackNum]==0;
}
};