[Java] 13ms...short if/else


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

Log in to reply
 

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