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
LeetCode605. Can Place Flowers
最新推荐文章于 2022-07-24 10:25:56 发布