题目描述
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.
Example:
1 | Input: s = 7, nums = [2,3,1,2,4,3] |
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
题目要求在一串数字中找到最短的连续数字,让他们的和大于给定的正数。
这里需要记录数组的开始和结尾的索引,并且需要记录连续的数字之和。每当数字比给定目标小的时候,右边的索引要往右移动,其他情况则左边索引向右移动一格,并且如果比最小的结果更小的时候需要记录这个长度。
代码实现
1 | class Solution: |