Files
2020-08-07 17:06:53 +08:00

27 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [209. Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/)
## 题目
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:
```c
Input: s = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
```
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).
## 题目大意
给定一个整型数组和一个数字 s找到数组中最短的一个连续子数组使得连续子数组的数字之和 sum>=s返回最短的连续子数组的返回值。
## 解题思路
这一题的解题思路是用滑动窗口。在滑动窗口 [i,j]之间不断往后移动,如果总和小于 s就扩大右边界 j不断加入右边的值直到 sum > s之和再缩小 i 的左边界,不断缩小直到 sum < s这时候右边界又可以往右移动以此类推