下面是中间有0的情况下,可以种植的个数
1 0
2 0
3 1
4 1
5 2
6 2
7 3
8 3
9 4
两边边界问题,左边我使用 begin
往后挪了两位后认为是1,右边往右挪了两位后算作1
func canPlaceFlowers(flowerbed []int, n int) bool {
if n <= 0 {
return true
}
flowerbed = append(flowerbed, 0, 1)
begin := -2
canPlantCnt := 0
for end, value := range flowerbed {
if value == 1 && end > begin {
curCnt := int((end-begin-2) / 2)
if curCnt > 0 {
canPlantCnt += curCnt
}
begin = end
}
}
return n <= canPlantCnt
}
标签:605,begin,end,curCnt,int,种花,leetcode
From: https://www.cnblogs.com/wudanyang/p/17018644.html