1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// @Title: 种花问题 (Can Place Flowers)
// @Author: 15816537946@163.com
// @Date: 2020-06-25 07:15:36
// @Runtime: 24 ms
// @Memory: 5.9 MB

func canPlaceFlowers(flowerBed []int, n int) bool {
	l := len(flowerBed)
	for i := range flowerBed {
		if flowerBed[i] == 0 &&
			((i+1 < l && flowerBed[i+1] == 0) || i+1 >= l) &&
			((i-1 >= 0 && flowerBed[i-1] == 0) || i-1 < 0) {
				 flowerBed[i] = 1
			n--
			if n <= 0 {
				return true
			}

		}
	}

	return n <= 0

}