Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

设计停车系统

题目

lettcode-1603题
难度:简单This is a picture without description

解题思路

每种车都有每种车所对应的一个车位,而且不能停到其他车的车位上面去,所以说我们需要三个变量将每种车的车位数量存起来。
然后在有新车进来时,判断车辆的类型,然后查找相应的停车位,如果有车位就返回一个true,相当于停车成功,然后该类型的车位减去一个,如果车位为0,则不允许停车,返回false

具体代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class ParkingSystem {

int big;
int medium;
int 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){
if (this.big>0){
this.big--;
return true;
}
}else if (carType==2){
if (this.medium>0){
this.medium--;
return true;
}
}else if (carType==3){
if (this.small>0){
this.small--;
return true;
}
}
return false;
}
}