leetcode 605. 种花问题
作者:互联网
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
if (flowerbed.size() == 1 && flowerbed[0] == 0){
return --n <= 0;
}
for (int i = 0; i < flowerbed.size(); i++){
if (flowerbed[i] == 0){
if (i == 0){//左边界
if (flowerbed[i + 1] == 0){
flowerbed[i] = 1;
n--;
}
}
else if (i == flowerbed.size()-1){//右边界
if (flowerbed[i - 1] == 0){
flowerbed[i] = 1;
n--;
}
}
else{
if ((flowerbed[i + 1] == 0) && (flowerbed[i - 1] == 0)){
flowerbed[i] = 1;
n--;
}
}
}
}
return n <= 0;
}
};
滴,新年卡,新的一年希望我可以考研上岸,也可以自己放过自己叭
标签:605,return,--,flowerbed,else,种花,&&,leetcode,size 来源: https://blog.csdn.net/qq_43180487/article/details/112060055