题目python
class Solution:
def canPlaceFlowers(self, flowerbed, n):
"""
:type flowerbed: List[int]
:type n: int
:rtype: bool
"""
count = 0
flowerbed.insert(0,0)
flowerbed.append(0)
for i,x in enumerate(flowerbed):
if x == 0:
count += 1
else:
count = 0
if count == 3:
count = 1
flowerbed[i -1] = 1
n -= 1
return n <= 0
思路:
加一个头,加一个尾,这样能够保证原来的列表中,开头和末尾均可以加入元素。app
从头开始,记录连续为0的数量,当计数count为3,那么中间位置插入一个1,此时能够种花的数量n减去1,而且0计数count从1开始。spa
一直到末尾,最终的n值是否小于0。code