Java easy come up with idea


  • 0
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
            if(n==0) return true;
            if((flowerbed.length/2)+1<n) return false;
            if(flowerbed.length==1&&flowerbed[0]==1) return false;
            if(flowerbed.length==1&&flowerbed[0]==0&&n==1) return true;
            for(int i=0;i<flowerbed.length;i++){
                if(i==0&&flowerbed[0]==0){
                    if(flowerbed[1]==0){
                        flowerbed[i]=1;
                        n--;
                    }
                    continue;
                }
                if(flowerbed[i]==1) continue;
                if(flowerbed[i]==0&&flowerbed[i-1]==0){
                    if((i+1==flowerbed.length)||(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.