My java solution


  • 0
    R
    public class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            boolean flag = true;
    		for (int i = 0; i < flowerbed.length; ++i) {
    			if (flowerbed[i] == 0) {
    				if (flag) {
    					n--;
    					flag = false;
    				} else
    					flag = true;
    			} else {
    				if (!flag && flowerbed[i - 1] == 0)
    					n++;
    				flag = false;
    			}
    		}
    		return n <= 0;
        }
    }
    

Log in to reply
 

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