题解 | #长度最小的连续子数组#
长度最小的连续子数组
https://www.nowcoder.com/practice/10dd5f8c5d984aa3bd69788d86aaef23
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param nums int整型一维数组
# @param target int整型
# @return int整型
#
class Solution:
def minSubarray(self, nums: List[int], target: int) -> int:
# write code here
n = len(nums)
ans = float("inf")
left = 0
sum = 0
for right in range(n):
sum += nums[right]
while sum >= target:
ans = min(ans, right - left + 1)
sum -= nums[left]
left += 1
return 0 if ans == float("inf") else ans
