File tree 6 files changed +170
-54
lines changed
solution/0600-0699/0605.Can Place Flowers
6 files changed +170
-54
lines changed Original file line number Diff line number Diff line change 41
41
42
42
<!-- 这里可写通用的实现逻辑 -->
43
43
44
+ ** 方法一:贪心**
45
+
44
46
<!-- tabs:start -->
45
47
46
48
### ** Python3**
47
49
48
50
<!-- 这里可写当前语言的特殊实现逻辑 -->
49
51
50
52
``` python
51
-
53
+ class Solution :
54
+ def canPlaceFlowers (self , flowerbed : List[int ], n : int ) -> bool :
55
+ flowerbed = [0 ] + flowerbed + [0 ]
56
+ for i in range (1 , len (flowerbed) - 1 ):
57
+ if sum (flowerbed[i - 1 : i + 2 ]) == 0 :
58
+ flowerbed[i] = 1
59
+ n -= 1
60
+ return n <= 0
52
61
```
53
62
54
63
### ** Java**
55
64
56
65
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
66
58
67
``` java
68
+ class Solution {
69
+ public boolean canPlaceFlowers (int [] flowerbed , int n ) {
70
+ int m = flowerbed. length;
71
+ for (int i = 0 ; i < m; ++ i) {
72
+ int l = i == 0 ? 0 : flowerbed[i - 1 ];
73
+ int r = i == m - 1 ? 0 : flowerbed[i + 1 ];
74
+ if (l + flowerbed[i] + r == 0 ) {
75
+ flowerbed[i] = 1 ;
76
+ -- n;
77
+ }
78
+ }
79
+ return n <= 0 ;
80
+ }
81
+ }
82
+ ```
83
+
84
+ ### ** C++**
85
+
86
+ ``` cpp
87
+ class Solution {
88
+ public:
89
+ bool canPlaceFlowers(vector<int >& flowerbed, int n) {
90
+ int m = flowerbed.size();
91
+ for (int i = 0; i < m; ++i)
92
+ {
93
+ int l = i == 0 ? 0 : flowerbed[ i - 1] ;
94
+ int r = i == m - 1 ? 0 : flowerbed[ i + 1] ;
95
+ if (l + flowerbed[ i] + r == 0)
96
+ {
97
+ flowerbed[ i] = 1;
98
+ --n;
99
+ }
100
+ }
101
+ return n <= 0;
102
+ }
103
+ };
104
+ ```
59
105
106
+ ### **Go**
107
+
108
+ ```go
109
+ func canPlaceFlowers(flowerbed []int, n int) bool {
110
+ m := len(flowerbed)
111
+ for i, v := range flowerbed {
112
+ l, r := 0, 0
113
+ if i > 0 {
114
+ l = flowerbed[i-1]
115
+ }
116
+ if i < m-1 {
117
+ r = flowerbed[i+1]
118
+ }
119
+ if l+v+r == 0 {
120
+ flowerbed[i] = 1
121
+ n--
122
+ }
123
+ }
124
+ return n <= 0
125
+ }
60
126
```
61
127
62
128
### ** ...**
Original file line number Diff line number Diff line change 33
33
### ** Python3**
34
34
35
35
``` python
36
-
36
+ class Solution :
37
+ def canPlaceFlowers (self , flowerbed : List[int ], n : int ) -> bool :
38
+ flowerbed = [0 ] + flowerbed + [0 ]
39
+ for i in range (1 , len (flowerbed) - 1 ):
40
+ if sum (flowerbed[i - 1 : i + 2 ]) == 0 :
41
+ flowerbed[i] = 1
42
+ n -= 1
43
+ return n <= 0
37
44
```
38
45
39
46
### ** Java**
40
47
41
48
``` java
49
+ class Solution {
50
+ public boolean canPlaceFlowers (int [] flowerbed , int n ) {
51
+ int m = flowerbed. length;
52
+ for (int i = 0 ; i < m; ++ i) {
53
+ int l = i == 0 ? 0 : flowerbed[i - 1 ];
54
+ int r = i == m - 1 ? 0 : flowerbed[i + 1 ];
55
+ if (l + flowerbed[i] + r == 0 ) {
56
+ flowerbed[i] = 1 ;
57
+ -- n;
58
+ }
59
+ }
60
+ return n <= 0 ;
61
+ }
62
+ }
63
+ ```
64
+
65
+ ### ** C++**
66
+
67
+ ``` cpp
68
+ class Solution {
69
+ public:
70
+ bool canPlaceFlowers(vector<int >& flowerbed, int n) {
71
+ int m = flowerbed.size();
72
+ for (int i = 0; i < m; ++i)
73
+ {
74
+ int l = i == 0 ? 0 : flowerbed[ i - 1] ;
75
+ int r = i == m - 1 ? 0 : flowerbed[ i + 1] ;
76
+ if (l + flowerbed[ i] + r == 0)
77
+ {
78
+ flowerbed[ i] = 1;
79
+ --n;
80
+ }
81
+ }
82
+ return n <= 0;
83
+ }
84
+ };
85
+ ```
42
86
87
+ ### **Go**
88
+
89
+ ```go
90
+ func canPlaceFlowers(flowerbed []int, n int) bool {
91
+ m := len(flowerbed)
92
+ for i, v := range flowerbed {
93
+ l, r := 0, 0
94
+ if i > 0 {
95
+ l = flowerbed[i-1]
96
+ }
97
+ if i < m-1 {
98
+ r = flowerbed[i+1]
99
+ }
100
+ if l+v+r == 0 {
101
+ flowerbed[i] = 1
102
+ n--
103
+ }
104
+ }
105
+ return n <= 0
106
+ }
43
107
```
44
108
45
109
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
bool canPlaceFlowers (vector<int >& flowerbed, int n) {
4
- int i = 0 , j = flowerbed.size ()-1 ;
5
- int cnt=0 ;
6
- int num ;
7
- while (i <= j && !flowerbed[i])
8
- ++i ;
9
- if (i > j)
10
- return n <= 1 + (j>>1 ) ;
11
- num = i>>1 ;
12
- while (!flowerbed[j])
13
- --j ;
14
- num += (flowerbed.size ()-1 -j)>>1 ;
15
- // cout << i << ' ' << j << endl ;
16
- while (i <= j)
4
+ int m = flowerbed.size ();
5
+ for (int i = 0 ; i < m; ++i)
17
6
{
18
-
19
- // cout << "num = " << num << ", cnt = " << cnt << endl ;
20
- if (flowerbed[i])
7
+ int l = i == 0 ? 0 : flowerbed[i - 1 ];
8
+ int r = i == m - 1 ? 0 : flowerbed[i + 1 ] ;
9
+ if (l + flowerbed[i] + r == 0 )
21
10
{
22
- if (cnt > 0 )
23
- num += (cnt-1 ) >> 1 ;
24
- cnt = 0 ;
11
+ flowerbed[i] = 1 ;
12
+ --n;
25
13
}
26
- else
27
- cnt++ ;
28
- ++i ;
29
14
}
30
- // cout << num << endl ;
31
- return num >= n ;
15
+ return n <= 0 ;
32
16
}
33
17
};
Original file line number Diff line number Diff line change
1
+ func canPlaceFlowers (flowerbed []int , n int ) bool {
2
+ m := len (flowerbed )
3
+ for i , v := range flowerbed {
4
+ l , r := 0 , 0
5
+ if i > 0 {
6
+ l = flowerbed [i - 1 ]
7
+ }
8
+ if i < m - 1 {
9
+ r = flowerbed [i + 1 ]
10
+ }
11
+ if l + v + r == 0 {
12
+ flowerbed [i ] = 1
13
+ n --
14
+ }
15
+ }
16
+ return n <= 0
17
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public boolean canPlaceFlowers (int [] flowerbed , int n ) {
3
- int len = flowerbed .length ;
4
- int cnt = 0 ;
5
- for ( int i = 0 ; i < len ; ++ i ) {
6
- if ( flowerbed [ i ] == 0 && ( i == 0 || flowerbed [ i - 1 ] == 0 ) && ( i == len - 1 || flowerbed [i + 1 ] == 0 )) {
7
- ++ cnt ;
3
+ int m = flowerbed .length ;
4
+ for ( int i = 0 ; i < m ; ++ i ) {
5
+ int l = i == 0 ? 0 : flowerbed [ i - 1 ];
6
+ int r = i == m - 1 ? 0 : flowerbed [i + 1 ];
7
+ if ( l + flowerbed [ i ] + r == 0 ) {
8
8
flowerbed [i ] = 1 ;
9
+ --n ;
9
10
}
10
11
}
11
- return cnt >= n ;
12
+ return n <= 0 ;
12
13
}
13
14
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
- def canPlaceFlowers (self , flowerBed , n ):
3
- """
4
- type flowerBed : List[int], n : int
5
- rtype : bool
6
- """
7
-
8
- i = 0
9
- while n > 0 and i < len (flowerBed ):
10
- if i == 0 and flowerBed [0 ] == 0 : # for 1st Element
11
- if len (flowerBed ) == 1 or (len (flowerBed ) > 1 and flowerBed [1 ] == 0 ):
12
- n -= 1
13
- flowerBed [0 ] = 1
14
- elif (
15
- i == len (flowerBed ) - 1 and flowerBed [i ] == 0 and flowerBed [i - 1 ] == 0
16
- ): # for last element
2
+ def canPlaceFlowers (self , flowerbed : List [int ], n : int ) -> bool :
3
+ flowerbed = [0 ] + flowerbed + [0 ]
4
+ for i in range (1 , len (flowerbed ) - 1 ):
5
+ if sum (flowerbed [i - 1 : i + 2 ]) == 0 :
6
+ flowerbed [i ] = 1
17
7
n -= 1
18
- flowerBed [i ] = 1
19
- elif flowerBed [i ] == 0 and flowerBed [i - 1 ] == 0 and flowerBed [i + 1 ] == 0 :
20
- n -= 1
21
- flowerBed [i ] = 1
22
- i += 1
23
-
24
- return n == 0
8
+ return n <= 0
You can’t perform that action at this time.
0 commit comments