1603. Design Parking System
常规解法:

class ParkingSystem {
public:
    ParkingSystem(int big, int medium, int small) {
        this->big = big;
        this->medium = medium;
        this->small = small;
    }
    
    bool addCar(int carType) {
        switch(carType)
        {
            case 1:
                if(big > 0){big--;return true;}else return false;break;
            case 2:
                if(medium > 0){medium--;return true;}else return false;break;
            case 3:
                if(small > 0){small--;return true;}else return false;break;
        }
        
        return false;
    }
private:
    int big;
    int medium;
    int small;
};

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * ParkingSystem* obj = new ParkingSystem(big, medium, small);
 * bool param_1 = obj->addCar(carType);
 */

上面的代码非常啰嗦,看了讨论区大神的代码,非常精简:

class ParkingSystem {
public:
    ParkingSystem(int big, int medium, int small) {
        count = {big,medium,small};
    }
    
    bool addCar(int carType) {
        return count[carType - 1]-- > 0;
    }
private:
    vector<int> count;
};

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * ParkingSystem* obj = new ParkingSystem(big, medium, small);
 * bool param_1 = obj->addCar(carType);
 */

上面的解法直接通过数组初始化vector,然后根据下标判断对应类型值是否大于0即可。