LeetCode605-种花问题

LeetCode605-种花问题

1
2
3
4
5
6
7
8
9
10
11
12
13
public boolean canPlaceFlowers(int[] flowerbed, int n) {
for (int i = 0, len = flowerbed.length; i < len && n > 0;) {
if (flowerbed[i] == 1) {
i += 2;
} else if (i == flowerbed.length - 1 || flowerbed[i + 1] == 0) {
n--;
i += 2;
} else {
i += 3;
}
}
return n <= 0;
}