解法一:贪心
从头往后遍历,如果相邻位置没有种花就在该位置上种花。注意考虑边界情况。
class Solution {public boolean canPlaceFlowers(int[] flowerbed, int n) {final int len = flowerbed.length;for (int i = 0; i < len && n > 0; ++i) {if (flowerbed[i] == 0) {if ((i - 1 < 0 || flowerbed[i - 1] == 0) &&(i + 1 >= len || flowerbed[i + 1] == 0)) {--n;flowerbed[i] = 1;}}}return n == 0;}}
