LeetCode 605. 种花问题
原题链接
简单
作者:
我是java同学
,
2023-10-03 15:25:20
,
所有人可见
,
阅读 64
class Solution {
public:
bool canPlaceFlowers(vector<int>& f, int n) {
int res = 0;
int m = f.size();
for (int i = 0; i < m; ) {
if (!f[i] && !f[max(i - 1, 0)] && !f[min(m - 1, i + 1)]) {
res ++ , i += 2;
}
else i ++ ;
}
cout << res << endl;
return res >= n;
}
};
class Solution {
public:
bool canPlaceFlowers(vector<int>& flowerbed, int n) {
flowerbed.insert(flowerbed.begin(), 0);
flowerbed.push_back(0);
for (int i = 1; i + 1 < flowerbed.size(); i ++ )
if (!flowerbed[i - 1] && !flowerbed[i] && !flowerbed[i + 1])
flowerbed[i] = 1, n -- ;
return n <= 0;
}
};