class Solution {
public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        int size = flowerbed.size();
        
        for(int i = 0;i < size;i++)
        {
            if(flowerbed[i] == 1) continue;
            //需要特殊处理第一个和最后一个
            int prev = (i == 0) ? 0:flowerbed[i-1];
            int next = (i == size - 1) ?0 :flowerbed[i+1];
            
            if(prev + next == 0)
            {
                flowerbed[i] = 1;
                n--;
            }
                
        }
        
        return n <= 0;
    }
};

运行效率:

Runtime: 12 ms, faster than 97.17% of C++ online submissions for Can Place Flowers.
Memory Usage: 20.3 MB, less than 62.95% of C++ online submissions for Can Place Flowers.