1437. 是否所有 1 都至少相隔 k 个元素

题目链接:https://leetcode-cn.com/problems/check-if-all-1s-are-at-least-length-k-places-away/

执行用时:1 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:48.3 MB, 在所有 Java 提交中击败了51.53% 的用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public boolean kLengthApart(int[] nums, int k) {
// 设置初始间距
int distance = k;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
if (distance < k) {
return false;
} else {
distance = 0;
}
} else {
distance++;
}
}
return true;
}
}

1437. 是否所有 1 都至少相隔 k 个元素
https://pisces34.github.io/2022/01/17/leetcode/1437/
发布于
2022年1月17日
许可协议