Skip to content

Commit 88a0452

Browse files
committed
Add solution 605
1 parent 0392f63 commit 88a0452

File tree

6 files changed

+210
-3
lines changed

6 files changed

+210
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package leetcode
2+
3+
func canPlaceFlowers(flowerbed []int, n int) bool {
4+
lenth := len(flowerbed)
5+
for i := 0; i < lenth && n > 0; i += 2 {
6+
if flowerbed[i] == 0 {
7+
if i+1 == lenth || flowerbed[i+1] == 0 {
8+
n--
9+
} else {
10+
i++
11+
}
12+
}
13+
}
14+
if n == 0 {
15+
return true
16+
}
17+
return false
18+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question605 struct {
9+
para605
10+
ans605
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para605 struct {
16+
flowerbed []int
17+
n int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans605 struct {
23+
one bool
24+
}
25+
26+
func Test_Problem605(t *testing.T) {
27+
28+
qs := []question605{
29+
30+
{
31+
para605{[]int{1, 0, 0, 0, 1}, 1},
32+
ans605{true},
33+
},
34+
35+
{
36+
para605{[]int{1, 0, 0, 0, 1}, 2},
37+
ans605{false},
38+
},
39+
40+
{
41+
para605{[]int{1, 0, 0, 0, 0, 1}, 2},
42+
ans605{false},
43+
},
44+
45+
{
46+
para605{[]int{0, 0, 1, 0}, 1},
47+
ans605{true},
48+
},
49+
50+
{
51+
para605{[]int{0, 0, 1, 0, 0}, 1},
52+
ans605{true},
53+
},
54+
55+
{
56+
para605{[]int{1, 0, 0, 1, 0}, 2},
57+
ans605{false},
58+
},
59+
}
60+
61+
fmt.Printf("------------------------Leetcode Problem 605------------------------\n")
62+
63+
for _, q := range qs {
64+
_, p := q.ans605, q.para605
65+
fmt.Printf("【input】:%v 【output】:%v\n", p, canPlaceFlowers(p.flowerbed, p.n))
66+
}
67+
fmt.Printf("\n\n\n")
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)
2+
3+
## 题目
4+
5+
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots.
6+
7+
Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return *if* `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: flowerbed = [1,0,0,0,1], n = 1
13+
Output: true
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: flowerbed = [1,0,0,0,1], n = 2
20+
Output: false
21+
```
22+
23+
**Constraints:**
24+
25+
- `1 <= flowerbed.length <= 2 * 104`
26+
- `flowerbed[i]` is `0` or `1`.
27+
- There are no two adjacent flowers in `flowerbed`.
28+
- `0 <= n <= flowerbed.length`
29+
30+
## 题目大意
31+
32+
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
33+
34+
## 解题思路
35+
36+
- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0,例如,00001 和 10000,第二种情况是 2 个 1 中间存在的 0 不足以种花,例如,1001 和 100001,1001 不能种任何花,100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。
37+
- 换个思路,找到可以种花的基本单元是 00,那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0,也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。
38+
39+
## 代码
40+
41+
```go
42+
package leetcode
43+
44+
func canPlaceFlowers(flowerbed []int, n int) bool {
45+
lenth := len(flowerbed)
46+
for i := 0; i < lenth && n > 0; i += 2 {
47+
if flowerbed[i] == 0 {
48+
if i+1 == lenth || flowerbed[i+1] == 0 {
49+
n--
50+
} else {
51+
i++
52+
}
53+
}
54+
}
55+
if n == 0 {
56+
return true
57+
}
58+
return false
59+
}
60+
```

leetcode/0910.Smallest-Range-II/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ Return the smallest possible difference between the maximum value of `B` and t
1010

1111
**Example 1:**
1212

13-
```
13+
```c
1414
Input: A = [1], K = 0
1515
Output: 0
1616
Explanation: B = [1]
1717
```
1818

1919
**Example 2:**
2020

21-
```
21+
```c
2222
Input: A = [0,10], K = 2
2323
Output: 6
2424
Explanation: B = [2,8]
2525
```
2626

2727
**Example 3:**
2828

29-
```
29+
```c
3030
Input: A = [1,3,6], K = 3
3131
Output: 3
3232
Explanation: B = [4,6,3]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)
2+
3+
## 题目
4+
5+
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in **adjacent** plots.
6+
7+
Given an integer array `flowerbed` containing `0`'s and `1`'s, where `0` means empty and `1` means not empty, and an integer `n`, return *if* `n` new flowers can be planted in the `flowerbed` without violating the no-adjacent-flowers rule.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: flowerbed = [1,0,0,0,1], n = 1
13+
Output: true
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: flowerbed = [1,0,0,0,1], n = 2
20+
Output: false
21+
```
22+
23+
**Constraints:**
24+
25+
- `1 <= flowerbed.length <= 2 * 104`
26+
- `flowerbed[i]` is `0` or `1`.
27+
- There are no two adjacent flowers in `flowerbed`.
28+
- `0 <= n <= flowerbed.length`
29+
30+
## 题目大意
31+
32+
假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。
33+
34+
## 解题思路
35+
36+
- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0,例如,00001 和 10000,第二种情况是 2 个 1 中间存在的 0 不足以种花,例如,1001 和 100001,1001 不能种任何花,100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。
37+
- 换个思路,找到可以种花的基本单元是 00,那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0,也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。
38+
39+
## 代码
40+
41+
```go
42+
package leetcode
43+
44+
func canPlaceFlowers(flowerbed []int, n int) bool {
45+
lenth := len(flowerbed)
46+
for i := 0; i < lenth && n > 0; i += 2 {
47+
if flowerbed[i] == 0 {
48+
if i+1 == lenth || flowerbed[i+1] == 0 {
49+
n--
50+
} else {
51+
i++
52+
}
53+
}
54+
}
55+
if n == 0 {
56+
return true
57+
}
58+
return false
59+
}
60+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ headless: true
322322
- [0594.Longest-Harmonious-Subsequence]({{< relref "/ChapterFour/0594.Longest-Harmonious-Subsequence.md" >}})
323323
- [0598.Range-Addition-II]({{< relref "/ChapterFour/0598.Range-Addition-II.md" >}})
324324
- [0599.Minimum-Index-Sum-of-Two-Lists]({{< relref "/ChapterFour/0599.Minimum-Index-Sum-of-Two-Lists.md" >}})
325+
- [0605.Can-Place-Flowers]({{< relref "/ChapterFour/0605.Can-Place-Flowers.md" >}})
325326
- [0628.Maximum-Product-of-Three-Numbers]({{< relref "/ChapterFour/0628.Maximum-Product-of-Three-Numbers.md" >}})
326327
- [0632.Smallest-Range-Covering-Elements-from-K-Lists]({{< relref "/ChapterFour/0632.Smallest-Range-Covering-Elements-from-K-Lists.md" >}})
327328
- [0633.Sum-of-Square-Numbers]({{< relref "/ChapterFour/0633.Sum-of-Square-Numbers.md" >}})

0 commit comments

Comments
 (0)