Re: [闲聊] 每日leetcode

楼主: dont   2024-11-16 10:40:01
3254. Find the Power of K-Size Subarrays I
## 思路
left = subarray的起始idx
扫阵列检查 当前的num是否为前一数+1, 不是就更新left
## Code
```python
class Solution:
def resultsArray(self, nums: List[int], k: int) -> List[int]:
if k == 1:
return nums
res = []
n = len(nums)
left = 0
for i in range(1, n):
if nums[i] != nums[i-1] + 1:
left = i
if i + 1 < k:
continue
res.append(nums[i] if i - k + 1 >= left else -1)
return res
```

Links booklink

Contact Us: admin [ a t ] ucptt.com