跳至主要内容

1437. Check If All 1's Are at Least Length K Places Away

class Solution {
public:
bool kLengthApart(vector<int>& nums, int k)
{
int cnt = k;
for (int num : nums)
{
if (num == 1)
{
if (cnt < k)
{
return false;
}
cnt = 0;
}
else
{
++cnt;
}
}
return true;
}
};
  • T: O(n)O(n)
  • S: O(1)O(1)