leetcode 1603. 设计停车系统
請(qǐng)你給一個(gè)停車(chē)場(chǎng)設(shè)計(jì)一個(gè)停車(chē)系統(tǒng)。停車(chē)場(chǎng)總共有三種不同大小的車(chē)位:大,中和小,每種尺寸分別有固定數(shù)目的車(chē)位。
請(qǐng)你實(shí)現(xiàn) ParkingSystem 類(lèi):
ParkingSystem(int big, int medium, int small) 初始化 ParkingSystem 類(lèi),三個(gè)參數(shù)分別對(duì)應(yīng)每種停車(chē)位的數(shù)目。
bool addCar(int carType) 檢查是否有 carType 對(duì)應(yīng)的停車(chē)位。 carType 有三種類(lèi)型:大,中,小,分別用數(shù)字 1, 2 和 3 表示。一輛車(chē)只能停在 carType 對(duì)應(yīng)尺寸的停車(chē)位中。如果沒(méi)有空車(chē)位,請(qǐng)返回 false ,否則將該車(chē)停入車(chē)位并返回 true 。
示例 1:
輸入:
[“ParkingSystem”, “addCar”, “addCar”, “addCar”, “addCar”]
[[1, 1, 0], [1], [2], [3], [1]]
輸出:
[null, true, true, false, false]
解釋:
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // 返回 true ,因?yàn)橛?1 個(gè)空的大車(chē)位
parkingSystem.addCar(2); // 返回 true ,因?yàn)橛?1 個(gè)空的中車(chē)位
parkingSystem.addCar(3); // 返回 false ,因?yàn)闆](méi)有空的小車(chē)位
parkingSystem.addCar(1); // 返回 false ,因?yàn)闆](méi)有空的大車(chē)位,唯一一個(gè)大車(chē)位已經(jīng)被占據(jù)了
代碼
class ParkingSystem {int big,medium,small;public ParkingSystem(int big, int medium, int small) {this.big = big;this.medium = medium;this.small = small;}public boolean addCar(int carType) {if(carType==1)return --big>=0;else if(carType==2)return --medium>=0;else return --small>=0;}}/*** Your ParkingSystem object will be instantiated and called as such:* ParkingSystem obj = new ParkingSystem(big, medium, small);* boolean param_1 = obj.addCar(carType);*/總結(jié)
以上是生活随笔為你收集整理的leetcode 1603. 设计停车系统的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到白色的龙在天上飞是什么意思
- 下一篇: 深入理解InnoDB(5)-文件系统