标签:inf leetcode turn tco src ima continue loading vector
思路:防御式编程,在收尾都加1个0,这样方便循环
bool canPlaceFlowers(vector<int> &flowerbed, int n)
{
if (n == 0)
return true;
if (flowerbed.size() == 0)
return false;
flowerbed.push_back(0);
flowerbed.insert(flowerbed.begin(), 0);
int len = flowerbed.size();
for (int i = 1; i < len - 1; i++)
{
if (flowerbed[i] == 1)
continue;
if (flowerbed[i - 1] == 0 && flowerbed[i + 1] == 0)
{
n--;
if (n == 0)
return true;
flowerbed[i] = 1;
continue;
}
}
return false;
}
新年第一道Leetcode题:605.种花问题(can-place-flowers)
标签:inf leetcode turn tco src ima continue loading vector
原文地址:https://www.cnblogs.com/Luweir/p/14220108.html