-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03.js
More file actions
30 lines (28 loc) · 909 Bytes
/
Copy path03.js
File metadata and controls
30 lines (28 loc) · 909 Bytes
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
// 种花问题 605题
/**
* @param {number[]} flowerbed
* @param {number} n
* @return {boolean}
*/
var canPlaceFlowers = function (flowerbed, n) {
let max = 0; // 定义计数器
for (let i = 0, len = flowerbed.length; i < len; i++) {
if (flowerbed[i] === 0) {
if ((i === 0 && flowerbed[1] === 0)||len===1) { // 左边界问题, 情况 [0] 或者[0,0]等等情况
max++;
i += 1;
} else if (flowerbed[i - 1] === 0 && flowerbed[i + 1] === 0) {
max++;
i += 1;
} else if (i===len-1 && flowerbed[len - 2] === 0) { // 右边界问题
max++;
i += 1
}
}
}
console.log(max)
return max >= n
};
/* console.log(canPlaceFlowers([1, 0, 0, 0, 1, 0, 0], 2)) */
console.log(canPlaceFlowers([0], 1))
export default canPlaceFlowers;