面试题 03.01
Solutions
class TripleInOne {
public:
vector<int> nums;
int indexes[3] = {0, 1, 2};
TripleInOne(int stackSize) : nums(stackSize * 3) {
}
void push(int stackNum, int value) {
if (indexes[stackNum] < nums.size()) {
nums[indexes[stackNum]] = value;
indexes[stackNum] += 3;
}
}
int pop(int stackNum) {
if (!isEmpty(stackNum)) {
indexes[stackNum] -= 3;
return nums[indexes[stackNum]];
}
else
return -1;
}
int peek(int stackNum) {
if (!isEmpty(stackNum)) {
return nums[indexes[stackNum] - 3];
}
else
return -1;
}
bool isEmpty(int stackNum) {
return indexes[stackNum] < 3;
}
};
/**
* 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);
*/Last updated