力扣-面试题 03.01 三合一(C++)- 数组实现栈

题目链接:https://leetcode-cn.com/problems/three-in-one-lcci/
题目如下:
力扣-面试题 03.01 三合一(C++)- 数组实现栈_第1张图片

class TripleInOne {
public:
    vector<int> vtr;
    int count[3];
    int size;
    TripleInOne(int stackSize) {
        vtr.resize(3*stackSize);
        size=stackSize;
        count[0]=0;
        count[1]=size;
        count[2]=size*2;
    }
    
    void push(int stackNum, int value) {
        if(count[stackNum]==size*(stackNum+1)) return;//判满
        vtr[count[stackNum]++]=value;
    }
    
    int pop(int stackNum) {
        if(count[stackNum]!=size*stackNum) return vtr[--count[stackNum]];//判空
        else return -1;
    }
    
    int peek(int stackNum) {
        if(count[stackNum]!=size*stackNum) return vtr[count[stackNum]-1];
        else return -1;
    }
    
    bool isEmpty(int stackNum) {
        return count[stackNum]==stackNum*size;
    }
};

/**
 * 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);
 */

你可能感兴趣的:(#,简单题,leetcode,c++,算法)