605. 种花问题

class Solution:
def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:
if len(flowerbed) == 1:
if flowerbed[0] == 0:
return 1 >= n
else:
return 0 >= n
else:
i = 0
count = 0
while i < len(flowerbed):
if i == 0 and flowerbed[0] == 0 and flowerbed[1] == 0:
flowerbed[0] = 1
count += 1
i += 2
elif i < len(flowerbed) - 1 and flowerbed[i - 1] == 0 and flowerbed[i] == 0 and flowerbed[i + 1] == 0:
flowerbed[i] = 1
count += 1
i += 2
elif i == len(flowerbed) - 1 and flowerbed[i - 1] == 0 and flowerbed[i] == 0:
flowerbed[i] = 1
count += 1
i += 2
else:
i += 1
return count >= n

605. 种花问题

上一篇:多表查询


下一篇:jquery操作复选框(checkbox)的12个小技巧总结