-
Notifications
You must be signed in to change notification settings - Fork 6
/
Solution605.java
executable file
·37 lines (35 loc) · 973 Bytes
/
Solution605.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import java.util.Arrays;
/**
* Created by slade on 2019/6/26.
*/
public class Solution605 {
public boolean canPlaceFlowers(int[] flowerbed, int n) {
int[] tmp = new int[flowerbed.length+2];
tmp[0] = 0;
System.arraycopy(flowerbed,0,tmp,1,flowerbed.length);
tmp[tmp.length-1]=0;
int restPlace = 0;
for (int i =0;i<tmp.length-2;){
if (tmp[i]+tmp[i+1]+tmp[i+2]==0){
restPlace+=1;
i = i+2;
}else if (tmp[i+2]==1){
i=i+3;
}else if (tmp[i+1]==1){
i=i+2;
}else if (tmp[i]==1){
i=i+1;
}
}
if (restPlace>=n){
return true;
}else {
return false;
}
}
public static void main(String[] args) {
int[] f = {0,0,0,1,0};
Solution605 s = new Solution605();
System.out.println(s.canPlaceFlowers(f,2));
}
}