mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 17:44:10 +08:00
Add solution 1437
This commit is contained in:
@ -52,6 +52,11 @@ func Test_Problem220(t *testing.T) {
|
||||
para220{[]int{1, 5, 9, 1, 5, 9}, 2, 3},
|
||||
ans220{false},
|
||||
},
|
||||
|
||||
{
|
||||
para220{[]int{1, 2, 1, 1}, 1, 0},
|
||||
ans220{true},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 220------------------------\n")
|
||||
|
@ -0,0 +1,14 @@
|
||||
package leetcode
|
||||
|
||||
func kLengthApart(nums []int, k int) bool {
|
||||
prevIndex := -1
|
||||
for i, num := range nums {
|
||||
if num == 1 {
|
||||
if prevIndex != -1 && i-prevIndex-1 < k {
|
||||
return false
|
||||
}
|
||||
prevIndex = i
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1437 struct {
|
||||
para1437
|
||||
ans1437
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1437 struct {
|
||||
nums []int
|
||||
k int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1437 struct {
|
||||
one bool
|
||||
}
|
||||
|
||||
func Test_Problem1437(t *testing.T) {
|
||||
|
||||
qs := []question1437{
|
||||
|
||||
{
|
||||
para1437{[]int{1, 0, 0, 0, 1, 0, 0, 1}, 2},
|
||||
ans1437{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1437{[]int{1, 0, 0, 1, 0, 1}, 2},
|
||||
ans1437{false},
|
||||
},
|
||||
|
||||
{
|
||||
para1437{[]int{1, 1, 1, 1, 1}, 0},
|
||||
ans1437{true},
|
||||
},
|
||||
|
||||
{
|
||||
para1437{[]int{0, 1, 0, 1}, 1},
|
||||
ans1437{true},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1437------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1437, q.para1437
|
||||
fmt.Printf("【input】:%v ", p)
|
||||
fmt.Printf("【output】:%v \n", kLengthApart(p.nums, p.k))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,73 @@
|
||||
# [1437. Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given an array `nums` of 0s and 1s and an integer `k`, return `True` if all 1's are at least `k` places away from each other, otherwise return `False`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: nums = [1,0,0,0,1,0,0,1], k = 2
|
||||
Output: true
|
||||
Explanation: Each of the 1s are at least 2 places away from each other.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||

|
||||
|
||||
```
|
||||
Input: nums = [1,0,0,1,0,1], k = 2
|
||||
Output: false
|
||||
Explanation: The second 1 and third 1 are only one apart from each other.
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: nums = [1,1,1,1,1], k = 0
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: nums = [0,1,0,1], k = 1
|
||||
Output: true
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= nums.length <= 10^5`
|
||||
- `0 <= k <= nums.length`
|
||||
- `nums[i]` is `0` or `1`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个由若干 0 和 1 组成的数组 nums 以及整数 k。如果所有 1 都至少相隔 k 个元素,则返回 True ;否则,返回 False 。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。扫描一遍数组,遇到 1 的时候比较前一个 1 的下标索引,如果相隔小于 k 则返回 false。如果大于等于 k 就更新下标索引,继续循环。循环结束输出 true 即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func kLengthApart(nums []int, k int) bool {
|
||||
prevIndex := -1
|
||||
for i, num := range nums {
|
||||
if num == 1 {
|
||||
if prevIndex != -1 && i-prevIndex-1 < k {
|
||||
return false
|
||||
}
|
||||
prevIndex = i
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user