[Java] 1603. Design a parking system---quick learning condition judgment! ! !

Please design a parking system for a parking lot. There are three different sizes of parking spaces in the parking lot: large, medium and small. Each size has a fixed number of parking spaces.

Please implement the ParkingSystem class:

ParkingSystem(int big, int medium, int small) initializes the ParkingSystem class, and the three parameters correspond to the number of each kind of parking space.
bool addCar(int carType) Check if there is a parking space corresponding to carType. There are three types of carType: large, medium, and small, which are represented by numbers 1, 2 and 3 respectively. A car can only be parked in a parking space of the size corresponding to the carType. If there is no empty parking space, please return false, otherwise park the car in the parking space and return true.

Example 1:

Input:
[“ParkingSystem”, “addCar”, “addCar”, “addCar”, “addCar”]
[[1, 1, 0], [1], [2], [3], [1]]
Output:
[null, true, true, false, false]

Explanation:
ParkingSystem parkingSystem = new ParkingSystem(1, 1, 0);
parkingSystem.addCar(1); // returns true because there is 1 empty large parking space
parkingSystem.addCar(2); // returns true because there is 1 empty spaces in
parkingSystem.addCar (3); // returns false, because there is no empty spaces small
parkingSystem.addCar (1); // returns false, because there is no large empty parking spaces, only one big parking space has been occupied

prompt:

0 <= big, medium, small <= 1000
carType value is 1, 2 or 3
will call the addCar function at most 1000 times

代码:
 private int big;
	private int medium;
	private 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&&big>0) {
    
    
			big--;
			return true;
		}
		if(carType==2&&medium>0) {
    
    
			medium--;
			return true;
		}
		if(carType==3&&small>0) {
    
    
			small--;
			return true;
		}
    	return false;
    }

Guess you like

Origin blog.csdn.net/qq_44461217/article/details/115001533