每日一题ing,今天是个easy题力扣605. Can Place Flowers
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
int init=0,sum=0;
flowerbed.push_back(0);//右边界不用考虑
for(int i=0;i<flowerbed.size();i++)
{
if(flowerbed[i]==1)
init++;
}
if(flowerbed[1]==0)
flowerbed[0]=1;
for(int i=1;i<flowerbed.size()-1;i++)
{
if(flowerbed[i-1]!=1&&flowerbed[i+1]!=1)
flowerbed[i]=1;
}
for(int i=0;i<flowerbed.size();i++)
{
if(flowerbed[i]==1)
sum++;
}
if(sum-init>=n)
return true;
return false;
}
};