インタビューの質問03.01。3対1
アイデア:スタックのサイズを記録する
配列のメモリを開くint * a; a = new int [1000];
class TripleInOne {
public:
int *stack;
int top[3];
int stackSize;
TripleInOne(int stackSize):stackSize(stackSize) {
stack = new int[stackSize*3];//此处创建新内存卡了1小时
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;
}
};