LeetCode605. Can Place Flowers

class Solution:
    """
    :type flowerbed: List[int]
    :type n: int
    :rtype: bool
    """
    def canPlaceFlowers(self, flowerbed, n):
        i = sum = 0
        if n == 0:
            return True
        if flowerbed[0] == 0: 
            flowerbed.insert(0, 0)
            flowerbed.insert(0, 1)
        if flowerbed[-1] == 0: 
            flowerbed.extend([0, 1])
        for j in range(1, len(flowerbed)):
            if flowerbed[j] == 1:
                sum += (j - i) // 2 - 1
                i = j
        return sum >= n

猜你喜欢

转载自blog.csdn.net/withing1113/article/details/80077850