Add solution 605

This commit is contained in:
YDZ
2021-01-01 21:21:21 +08:00
parent 0392f638ae
commit 88a0452f26
6 changed files with 210 additions and 3 deletions

View File

@ -0,0 +1,18 @@
package leetcode
func canPlaceFlowers(flowerbed []int, n int) bool {
lenth := len(flowerbed)
for i := 0; i < lenth && n > 0; i += 2 {
if flowerbed[i] == 0 {
if i+1 == lenth || flowerbed[i+1] == 0 {
n--
} else {
i++
}
}
}
if n == 0 {
return true
}
return false
}

View File

@ -0,0 +1,68 @@
package leetcode
import (
"fmt"
"testing"
)
type question605 struct {
para605
ans605
}
// para 是参数
// one 代表第一个参数
type para605 struct {
flowerbed []int
n int
}
// ans 是答案
// one 代表第一个答案
type ans605 struct {
one bool
}
func Test_Problem605(t *testing.T) {
qs := []question605{
{
para605{[]int{1, 0, 0, 0, 1}, 1},
ans605{true},
},
{
para605{[]int{1, 0, 0, 0, 1}, 2},
ans605{false},
},
{
para605{[]int{1, 0, 0, 0, 0, 1}, 2},
ans605{false},
},
{
para605{[]int{0, 0, 1, 0}, 1},
ans605{true},
},
{
para605{[]int{0, 0, 1, 0, 0}, 1},
ans605{true},
},
{
para605{[]int{1, 0, 0, 1, 0}, 2},
ans605{false},
},
}
fmt.Printf("------------------------Leetcode Problem 605------------------------\n")
for _, q := range qs {
_, p := q.ans605, q.para605
fmt.Printf("【input】:%v 【output】:%v\n", p, canPlaceFlowers(p.flowerbed, p.n))
}
fmt.Printf("\n\n\n")
}

View File

@ -0,0 +1,60 @@
# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)
## 题目
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.
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.
**Example 1:**
```
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
```
**Example 2:**
```
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
```
**Constraints:**
- `1 <= flowerbed.length <= 2 * 104`
- `flowerbed[i]` is `0` or `1`.
- There are no two adjacent flowers in `flowerbed`.
- `0 <= n <= flowerbed.length`
## 题目大意
假设你有一个很长的花坛一部分地块种植了花另一部分却没有。可是花卉不能种植在相邻的地块上它们会争夺水源两者都会死去。给定一个花坛表示为一个数组包含0和1其中0表示没种植花1表示种植了花和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花能则返回True不能则返回False。
## 解题思路
- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0例如00001 和 10000第二种情况是 2 个 1 中间存在的 0 不足以种花例如1001 和 1000011001 不能种任何花100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。
- 换个思路,找到可以种花的基本单元是 00那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。
## 代码
```go
package leetcode
func canPlaceFlowers(flowerbed []int, n int) bool {
lenth := len(flowerbed)
for i := 0; i < lenth && n > 0; i += 2 {
if flowerbed[i] == 0 {
if i+1 == lenth || flowerbed[i+1] == 0 {
n--
} else {
i++
}
}
}
if n == 0 {
return true
}
return false
}
```

View File

@ -10,7 +10,7 @@ Return the smallest possible difference between the maximum value of `B` and t
**Example 1:** **Example 1:**
``` ```c
Input: A = [1], K = 0 Input: A = [1], K = 0
Output: 0 Output: 0
Explanation: B = [1] Explanation: B = [1]
@ -18,7 +18,7 @@ Explanation: B = [1]
**Example 2:** **Example 2:**
``` ```c
Input: A = [0,10], K = 2 Input: A = [0,10], K = 2
Output: 6 Output: 6
Explanation: B = [2,8] Explanation: B = [2,8]
@ -26,7 +26,7 @@ Explanation: B = [2,8]
**Example 3:** **Example 3:**
``` ```c
Input: A = [1,3,6], K = 3 Input: A = [1,3,6], K = 3
Output: 3 Output: 3
Explanation: B = [4,6,3] Explanation: B = [4,6,3]

View File

@ -0,0 +1,60 @@
# [605. Can Place Flowers](https://leetcode.com/problems/can-place-flowers/)
## 题目
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.
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.
**Example 1:**
```
Input: flowerbed = [1,0,0,0,1], n = 1
Output: true
```
**Example 2:**
```
Input: flowerbed = [1,0,0,0,1], n = 2
Output: false
```
**Constraints:**
- `1 <= flowerbed.length <= 2 * 104`
- `flowerbed[i]` is `0` or `1`.
- There are no two adjacent flowers in `flowerbed`.
- `0 <= n <= flowerbed.length`
## 题目大意
假设你有一个很长的花坛一部分地块种植了花另一部分却没有。可是花卉不能种植在相邻的地块上它们会争夺水源两者都会死去。给定一个花坛表示为一个数组包含0和1其中0表示没种植花1表示种植了花和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花能则返回True不能则返回False。
## 解题思路
- 这一题最容易想到的解法是步长为 2 遍历数组,依次计数 0 的个数。有 2 种特殊情况需要单独判断,第一种情况是首尾连续多个 0例如00001 和 10000第二种情况是 2 个 1 中间存在的 0 不足以种花例如1001 和 1000011001 不能种任何花100001 只能种一种花。单独判断出这 2 种情况,这一题就可以 AC 了。
- 换个思路,找到可以种花的基本单元是 00那么上面那 2 种特殊情况都可以统一成一种情况。判断是否当前存在 00 的组合,如果存在 00 的组合,都可以种花。末尾的情况需要单独判断,如果末尾为 0也可以种花。这个时候不需要再找 00 组合,因为会越界。代码实现如下,思路很简洁明了。
## 代码
```go
package leetcode
func canPlaceFlowers(flowerbed []int, n int) bool {
lenth := len(flowerbed)
for i := 0; i < lenth && n > 0; i += 2 {
if flowerbed[i] == 0 {
if i+1 == lenth || flowerbed[i+1] == 0 {
n--
} else {
i++
}
}
}
if n == 0 {
return true
}
return false
}
```

View File

@ -322,6 +322,7 @@ headless: true
- [0594.Longest-Harmonious-Subsequence]({{< relref "/ChapterFour/0594.Longest-Harmonious-Subsequence.md" >}}) - [0594.Longest-Harmonious-Subsequence]({{< relref "/ChapterFour/0594.Longest-Harmonious-Subsequence.md" >}})
- [0598.Range-Addition-II]({{< relref "/ChapterFour/0598.Range-Addition-II.md" >}}) - [0598.Range-Addition-II]({{< relref "/ChapterFour/0598.Range-Addition-II.md" >}})
- [0599.Minimum-Index-Sum-of-Two-Lists]({{< relref "/ChapterFour/0599.Minimum-Index-Sum-of-Two-Lists.md" >}}) - [0599.Minimum-Index-Sum-of-Two-Lists]({{< relref "/ChapterFour/0599.Minimum-Index-Sum-of-Two-Lists.md" >}})
- [0605.Can-Place-Flowers]({{< relref "/ChapterFour/0605.Can-Place-Flowers.md" >}})
- [0628.Maximum-Product-of-Three-Numbers]({{< relref "/ChapterFour/0628.Maximum-Product-of-Three-Numbers.md" >}}) - [0628.Maximum-Product-of-Three-Numbers]({{< relref "/ChapterFour/0628.Maximum-Product-of-Three-Numbers.md" >}})
- [0632.Smallest-Range-Covering-Elements-from-K-Lists]({{< relref "/ChapterFour/0632.Smallest-Range-Covering-Elements-from-K-Lists.md" >}}) - [0632.Smallest-Range-Covering-Elements-from-K-Lists]({{< relref "/ChapterFour/0632.Smallest-Range-Covering-Elements-from-K-Lists.md" >}})
- [0633.Sum-of-Square-Numbers]({{< relref "/ChapterFour/0633.Sum-of-Square-Numbers.md" >}}) - [0633.Sum-of-Square-Numbers]({{< relref "/ChapterFour/0633.Sum-of-Square-Numbers.md" >}})