605. Can Place Flowers [Easy] 数组隔0放1

it2022-05-12  50

605. Can Place Flowers

605. Can Place Flowers

刚开始想的统计相连的0的个数,但是首位需要特殊处理,比较麻烦。

class Solution(object): def canPlaceFlowers(self, flowerbed, n): """ :type flowerbed: List[int] :type n: int :rtype: bool """ cnt = 0 for i in range(len(flowerbed)): if flowerbed[i]==0 and (i==0 or flowerbed[i-1]==0) and (i==len(flowerbed)-1 or flowerbed[i+1]==0): flowerbed[i] = 1 cnt += 1 return cnt >= n

最新回复(0)