0%

Leetcode 1603. Design Parking System

題目

1
2
3
4
5
Input
["ParkingSystem", "addCar", "addCar", "addCar", "addCar"]
[[1, 1, 0], [1], [2], [3], [1]]
Output
[null, true, true, false, false]

解法思維

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var ParkingSystem = function(big, medium, small) {
this.big=big
this.medium=medium
this.small=small
};

ParkingSystem.prototype.addCar = function(carType) {
if(carType==1){
this.big-=1
return this.big>=0
}else if(carType==2){
this.medium-=1
return this.medium>=0
}else if(carType==3){
this.small-=1
return this.small>=0
}
}
1
2
3
4
5
6
var ParkingSystem = function(big, medium, small) {
this.remainingSpaces = [big, medium, small];
};
ParkingSystem.prototype.addCar = function(carType) {
return (this.remainingSpaces[carType - 1]--) > 0
};