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).
双指针
双指针解法如何确定双指针移动的方法和结束条件?
当区间i到j内的元素的和大于等于s时,不需要再考虑区间i到j+1,j+2,…,nums.size()-1内的元素之和,因为这里要求的是最短的连续子数组。[i,j+1],[i, j+2]等与[i, j]相比都不是无法满足最短这个条件。