Concise Jave Solution


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