simplest c++ code


  • 5
    A
    class Solution {
    public:
        bool canPlaceFlowers(vector<int>& flowerbed, int n) {
            flowerbed.insert(flowerbed.begin(),0);
            flowerbed.push_back(0);
            for(int i = 1; i < flowerbed.size()-1; ++i)
            {
                if(flowerbed[i-1] + flowerbed[i] + flowerbed[i+1] == 0)
                {
                    --n;
                    ++i;
                }
                    
            }
            return n <=0;
        }
    };
    

  • 0
    A
    This post is deleted!

  • 0
    L

    @acforu

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

Log in to reply
 

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