1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| // @Title: 种花问题 (Can Place Flowers)
// @Author: 15816537946@163.com
// @Date: 2021-01-02 20:24:16
// @Runtime: 16 ms
// @Memory: 6 MB
func canPlaceFlowers(flowerbed []int, n int) bool {
nLen := len(flowerbed)
// 每次跳两格
for i := 0; i < nLen; i += 2 {
// 如果当前为空格
if flowerbed[i] == 0 {
// 如果是最后一格或者下一格为空
if i == nLen-1 || flowerbed[i+1] == 0 {
n--
} else {
i++
}
}
}
return n <= 0
}
/*
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
// 每次跳两格
for (int i = 0; i < flowerbed.size(); i += 2) {
// 如果当前为空地
if (flowerbed[i] == 0) {
// 如果是最后一格或者下一格为空
if (i == flowerbed.size() - 1 || flowerbed[i + 1] == 0) {
n--;
} else {
i++;
}
}
}
return n <= 0;
}
};
*/
|