Simple JAVA AC Solution


  • 0
    W
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            if (n == 0) return true;
            int i = 0;
            while (i < flowerbed.length) {
                if (flowerbed[i] != 0) {
                    i++;
                    continue;
                }
                if (i > 0 && flowerbed[i - 1] != 0) {
                    i++;
                    continue;
                }
                if (i < flowerbed.length - 1 && flowerbed[i + 1] != 0) {
                    i++;
                    continue;
                }
                n--;
                if (n == 0) return true;
                flowerbed[i] = 1;
            }
            return false;
        }
    

Log in to reply
 

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