当前位置: 面试刷题>> 停车场 (经典算法题500道)


由于原始题目只给出了“停车场”这一关键词,并未明确具体的算法要求,我将假设一个常见的场景来补充完整题目描述,并给出相应的PHP、Python和JavaScript代码示例。

题目描述

设计一个停车场管理系统,该系统能够处理车辆的进入(park)和离开(unpark)操作。车辆由唯一的ID标识,停车场有一个固定的容量限制。系统需要能够:

  1. 车辆进入:如果停车场未满,允许车辆进入并返回其停放位置(从1开始编号的整数)。
  2. 车辆离开:如果停车场中有该车辆,则允许其离开,并返回该车辆离开后的第一个空位置编号(如果整个停车场都空了,则返回-1)。

示例代码

PHP 示例

class ParkingLot {
    private $capacity;
    private $slots;

    public function __construct($capacity) {
        $this->capacity = $capacity;
        $this->slots = array_fill(1, $capacity, null);
    }

    public function park($carId) {
        foreach ($this->slots as $key => $value) {
            if ($value === null) {
                $this->slots[$key] = $carId;
                return $key;
            }
        }
        return -1; // 停车场已满
    }

    public function unpark($carId) {
        $firstEmptySlot = null;
        foreach ($this->slots as $key => $value) {
            if ($value === $carId) {
                $this->slots[$key] = null;
                return $firstEmptySlot === null ? -1 : $firstEmptySlot;
            }
            if ($value === null && $firstEmptySlot === null) {
                $firstEmptySlot = $key;
            }
        }
        return -1; // 未找到车辆
    }
}

// 使用示例
$parkingLot = new ParkingLot(5);
echo $parkingLot->park(1) . "\n"; // 输出车辆停放位置
echo $parkingLot->unpark(1) . "\n"; // 输出车辆离开后第一个空位置

Python 示例

class ParkingLot:
    def __init__(self, capacity):
        self.capacity = capacity
        self.slots = [None] * capacity

    def park(self, car_id):
        for i, slot in enumerate(self.slots):
            if slot is None:
                self.slots[i] = car_id
                return i + 1
        return -1  # 停车场已满

    def unpark(self, car_id):
        first_empty_slot = None
        for i, slot in enumerate(self.slots):
            if slot == car_id:
                self.slots[i] = None
                return first_empty_slot if first_empty_slot is not None else -1
            if slot is None and first_empty_slot is None:
                first_empty_slot = i + 1
        return -1  # 未找到车辆

# 使用示例
parking_lot = ParkingLot(5)
print(parking_lot.park(1))  # 输出车辆停放位置
print(parking_lot.unpark(1))  # 输出车辆离开后第一个空位置

JavaScript 示例

class ParkingLot {
    constructor(capacity) {
        this.capacity = capacity;
        this.slots = new Array(capacity).fill(null);
    }

    park(carId) {
        for (let i = 0; i < this.slots.length; i++) {
            if (this.slots[i] === null) {
                this.slots[i] = carId;
                return i + 1;
            }
        }
        return -1; // 停车场已满
    }

    unpark(carId) {
        let firstEmptySlot = null;
        for (let i = 0; i < this.slots.length; i++) {
            if (this.slots[i] === carId) {
                this.slots[i] = null;
                return firstEmptySlot !== null ? firstEmptySlot : -1;
            }
            if (this.slots[i] === null && firstEmptySlot === null) {
                firstEmptySlot = i + 1;
            }
        }
        return -1; // 未找到车辆
    }
}

// 使用示例
const parkingLot = new ParkingLot(5);
console.log(parkingLot.park(1)); // 输出车辆停放位置
console.log(parkingLot.unpark(1)); // 输出车辆离开后第一个空位置

码小课:在码小课网站上,你可以找到更多关于数据结构、算法设计以及编程语言的深入解析和实战项目,帮助你更好地掌握编程技能。

推荐面试题