Leetcode 605 Can Place Flowers

来源:互联网 发布:sql count 用法 编辑:程序博客网 时间:2024/06/07 00:49

Leetcode 605 Can Place Flowers

#include <vector>using namespace std;class Solution {public:    bool canPlaceFlowers(vector<int>& flowerbed, int n)     {    int constantZero = 0;    int flowerCount = 0;    flowerbed.insert(flowerbed.begin(),0);    flowerbed.push_back(0);    int size = flowerbed.size();    for(int i = 0;i < size;i ++)    {        if(flowerbed [i] == 0)          constantZero ++;        else if(flowerbed[i] == 1)        {        flowerCount += (constantZero - 1) / 2;        constantZero = 0;        }    }    flowerCount += (constantZero - 1) / 2;    return (flowerCount >= n);    }};
原创粉丝点击