1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class Solution { public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int size = flowerbed.size(), i = 0, cnt = 0; while (i < size) { if (flowerbed[i]) { ++i; } else if ((i == 0 || !flowerbed[i - 1]) && (i == size - 1 || !flowerbed[i + 1])) { flowerbed[i] = 1; ++cnt; } if (cnt >= n) return true; ++i; } return false; } };
|