给定数组flowerbed表示一个花床,0表示位置为空,1表示位置非空。花不能相邻种植,即不能出现相邻的1。给定想要种植的花朵数目n,判断是否可以满足要求。
使用continue跳出循环
class Solution(object):
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
count = 0
for i,num in enumerate(flowerbed):
if num : continue
if i > 0 and flowerbed[i-1]: continue
if i < len(flowerbed) - 1 and flowerbed[i+1]:continue
count += 1
flowerbed[i] = 1
return count >= n