美文网首页
LeetCode-1603-设计停车系统

LeetCode-1603-设计停车系统

作者: 阿凯被注册了 | 来源:发表于2020-10-11 00:08 被阅读0次
image.png
解题思路:

Python3代码:
# v0
class ParkingSystem:

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

    def addCar(self, carType: int) -> bool:
        if carType == 1:
            if self.big>0:
                self.big -= 1
                return True 
            else:
                return False
        elif carType == 2:
            if self.medium > 0:
                self.medium -= 1
                return True
            else:
                return False
        elif carType == 3:
            if self.small > 0:
                self.small -= 1
                return True
            else:
                return False
# v1
class ParkingSystem:

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

    def addCar(self, carType: int) -> bool:
        if self.data[carType] == 0:
            return False
        else:
            self.data[carType] -= 1
            return True
# v2
class ParkingSystem:

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

    def addCar(self, carType: int) -> bool:
        self.data[carType] -= 1
        return self.data[carType] >= 0

相关文章

网友评论

      本文标题:LeetCode-1603-设计停车系统

      本文链接:https://www.haomeiwen.com/subject/voejpktx.html