在return里写条件语句
return 0 if ans == n + 1 else ans
参考
力扣:长度最小的子数组
class Solution: def minSubArrayLen(self, s: int, nums: List[int]) -> int: if not nums: return 0 n = len(nums) ans = n + 1 start, end = 0, 0 total = 0 while end < n: total += nums[end] while total >= s: ans = min(ans, end - start + 1) total -= nums[start] start += 1 end += 1 return 0 if ans == n + 1 else ans标签:end,nums,python,start,ans,return,total,巧用 From: https://www.cnblogs.com/Nnnome/p/17743549.html