列表

详情


1603. 设计停车系统

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

请你实现 ParkingSystem 类:

 

示例 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 ,因为有 1 个空的大车位
parkingSystem.addCar(2); // 返回 true ,因为有 1 个空的中车位
parkingSystem.addCar(3); // 返回 false ,因为没有空的小车位
parkingSystem.addCar(1); // 返回 false ,因为没有空的大车位,唯一一个大车位已经被占据了

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
class ParkingSystem { public: ParkingSystem(int big, int medium, int small) { } bool addCar(int carType) { } }; /** * Your ParkingSystem object will be instantiated and called as such: * ParkingSystem* obj = new ParkingSystem(big, medium, small); * bool param_1 = obj->addCar(carType); */

kotlin 解法, 执行用时: 336 ms, 内存消耗: 46.3 MB, 提交时间: 2023-10-11 11:03:04

class ParkingSystem(big: Int, medium: Int, small: Int) {
    private val park = intArrayOf(0, big, medium, small)
    
    fun addCar(carType: Int): Boolean = park[carType]-- > 0
}

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * var obj = ParkingSystem(big, medium, small)
 * var param_1 = obj.addCar(carType)
 */

rust 解法, 执行用时: 20 ms, 内存消耗: 2.3 MB, 提交时间: 2023-10-11 11:02:37

struct ParkingSystem {
    park: Vec<i32>,
}

/**
 * `&self` means the method takes an immutable reference.
 * If you need a mutable reference, change it to `&mut self` instead.
 */
impl ParkingSystem {
    fn new(big: i32, medium: i32, small: i32) -> Self {
        ParkingSystem { park: vec![0, big, medium, small] }
    }
    
    fn add_car(&mut self, car_type: i32) -> bool {
        self.park[car_type as usize] -= 1;
        self.park[car_type as usize] >= 0
    }
}

/**
 * Your ParkingSystem object will be instantiated and called as such:
 * let obj = ParkingSystem::new(big, medium, small);
 * let ret_1: bool = obj.add_car(carType);
 */

javascript 解法, 执行用时: 144 ms, 内存消耗: 48.7 MB, 提交时间: 2023-10-11 11:01:54

/**
 * @param {number} big
 * @param {number} medium
 * @param {number} small
 */
var ParkingSystem = function(big, medium, small) {
    this.big = big;
    this.medium = medium;
    this.small = small;
};

/** 
 * @param {number} carType
 * @return {boolean}
 */
ParkingSystem.prototype.addCar = function(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;
};

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

java 解法, 执行用时: 7 ms, 内存消耗: 43.4 MB, 提交时间: 2023-10-11 11:01:13

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

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

cpp 解法, 执行用时: 60 ms, 内存消耗: 32.8 MB, 提交时间: 2023-10-11 11:00:55

class ParkingSystem {
public:
    int b, m, s;
    ParkingSystem(int big, int medium, int small): b(big), m(medium), s(small) {}
    
    bool addCar(int carType) {
        if (carType == 1) {
            if (b > 0) {
                b--;
                return true;
            }
        } else if (carType == 2) {
            if (m > 0) {
                m--;
                return true;
            }
        } else if (carType == 3) {
            if (s > 0) {
                s--;
                return true;
            }
        }
        return false;
    }
};

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

python3 解法, 执行用时: 148 ms, 内存消耗: 15.1 MB, 提交时间: 2021-03-19 23:50:56

class ParkingSystem:

    def __init__(self, big: int, medium: int, small: int):
        self.cards = [big, medium, small]


    def addCar(self, carType: int) -> bool:

        if self.cards[carType-1] > 0:
            self.cards[carType-1] -= 1
            return True
        return False



# Your ParkingSystem object will be instantiated and called as such:
# obj = ParkingSystem(big, medium, small)
# param_1 = obj.addCar(carType)

golang 解法, 执行用时: 52 ms, 内存消耗: 7.2 MB, 提交时间: 2021-03-19 23:49:21

type ParkingSystem struct {
	cards []int
}


func Constructor(big int, medium int, small int) ParkingSystem {
	return ParkingSystem{cards: []int{big, medium, small}}
}


func (this *ParkingSystem) AddCar(carType int) bool {
	if this.cards[carType-1] > 0 {
		this.cards[carType-1]--
		return true
	}
	return false
}


/**
 * Your ParkingSystem object will be instantiated and called as such:
 * obj := Constructor(big, medium, small);
 * param_1 := obj.AddCar(carType);
 */

上一题