605. 种花问题

Posted W-forever

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. 种花问题的主要内容,如果未能解决你的问题,请参考以下文章

贪心605. 种花问题

605. 种花问题

605. 种花问题

数组605. 种花问题

Leetcode 605.种花问题

605. Can Place Flowers种花问题leetcode