力扣 2090. 半径为k的子数组平均值

使用前缀和 + 双指针实现

  1. 前缀和
1
2
3
curs = [0]
for n in nums:
curs.append(curs[-1] + n)
  1. 双指针
1
2
3
4
5
6
7
8
9
10
left, right, res, nums_len = -k - 1, k - 1, [], len(nums)

for _ in range(nums_len):
left += 1
right += 1
if left >= 0 and right < nums_len:
cur = curs[right + 1] - curs[left]
res.append(cur // (2 * k + 1))
else:
res.append(-1)
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信