[605. Can Place Flowers My Submissions] C++_AC_ Scan the vector


  • 0
    class Solution {
    public:
    bool canPlaceFlowers(vector<int>& flowerbed, int n) {
        for(int i = 0; i < flowerbed.size(); ++i){
            if(n == 0) return true;
            if(flowerbed[i] == 1) continue;
            //flowerbed[i] == 0
            if(i == 0 || (i >= 1 && flowerbed[i-1] == 0)){
                if(i == flowerbed.size()-1 || (i + 1 < flowerbed.size() && flowerbed[i+1] == 0)){
                    flowerbed[i] = 1;
                    n--;
                }
            }
        }
        return n == 0;
    }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.