第一种方法
var canPlaceFlowers = function(flowerbed, n) {
let num = 0
if(flowerbed.length==1){
if(flowerbed[0]==0){
num= 1
}else{
num= 0
}
}else{
for(let i = 0;i<flowerbed.length;i++){
if(i==0&&flowerbed[i]==0&&flowerbed[i+1]==0){
num++
flowerbed[i]=1
}else if(i==(flowerbed.length-1)&&flowerbed[i]==0&&flowerbed[i-1]==0){
num++
flowerbed[i]=1
}else if(flowerbed[i-1]==0&&flowerbed[i]==0&&flowerbed[i+1]==0){
num++
flowerbed[i]=1
}
}
}
return num>=n
};
第二种方法
var canPlaceFlowers = function(flowerbed, n) {
let num = 0
if(flowerbed.length==1){
if(flowerbed[0]==0){
num= 1
}else{
num= 0
}
}else{
for(let i = 0;i<flowerbed.length;i++){
if(i==0&&flowerbed[i]==0&&flowerbed[i+1]==0){
num++
flowerbed[i]=1
i++
}else if(i==(flowerbed.length-1)&&flowerbed[i]==0&&flowerbed[i-1]==0){
num++
flowerbed[i]=1
i++
}else if(flowerbed[i-1]==0&&flowerbed[i]==0&&flowerbed[i+1]==0){
num++
flowerbed[i]=1
i++
}
}
}
return num>=n
};