simple Java solution


  • 0
    B
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            int len = flowerbed.length;
            if(n==0)    return true;
            if(len==1&&flowerbed[0]==0)  return n==1;
            if(len==1&&flowerbed[0]==1)  return false;
            int count = 0;
            for(int i=0;i<len;i++){
                if(i==0){
                    if(flowerbed[i]==0&&flowerbed[i+1]==0) {
                        count++;
                        flowerbed[i]=1;
                    }
                }else if(i==len-1){
                    if(flowerbed[len-2]==0&&flowerbed[len-1]==0)    count++;
                }else if(flowerbed[i-1]==0&&flowerbed[i]==0&&flowerbed[i+1]==0){
                    count++;
                    flowerbed[i]=1;
                }
                if(count==n)    return true;
            }
            return false;
        }
    }```

Log in to reply
 

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