LeetCode 1603. 设计停车系统(模拟)

题意:

请你给一个停车场设计一个停车系统。停车场总共有三种不同大小的车位:大,中和小,每种尺寸分别有固定数目的车位。

请你实现 ParkingSystem 类:

ParkingSystem(int big, int medium, int small) 初始化 ParkingSystem 类,
三个参数分别对应每种停车位的数目。
bool addCar(int carType) 检查是否有 carType 对应的停车位。
 carType 有三种类型:大,中,小,分别用数字 123 表示。
 一辆车只能停在  carType 对应尺寸的停车位中。
 如果没有空车位,请返回 false ,否则将该车停入车位并返回 true 。

数据范围:
0 <= big, medium, small <= 1000
carType 取值为 123
最多会调用 addCar 函数 1000

解法:

开三个变量记录每种车位的个数即可.

code:

class ParkingSystem {
public:
    int cnt[4];
    ParkingSystem(int x, int y, int z) {
        cnt[1]=x,cnt[2]=y,cnt[3]=z;
    }
    
    bool addCar(int x) {
        if(cnt[x]>=1){
            cnt[x]--;
            return 1;
        }
        return 0;
    }
};

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


版权声明:本文为weixin_44178736原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。