We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
209.长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度。如果不存在符合条件的连续子数组,返回 0。
示例:
输入: s = 7, nums = [2,3,1,2,4,3] 输出: 2 解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。
进阶:
如果你已经完成了 O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法。
https://leetcode-cn.com/problems/minimum-size-subarray-sum/submissions
纯暴力的循环也就是穷举每种子数组并求和,当然是会超时的,这里就不做讲解了。下面这种解法会在暴力法的基础上稍作优化,具体的思路如下:
/** * @param {number} s * @param {number[]} nums * @return {number} */ let minSubArrayLen = function (s, nums) { let min = Infinity for (let i = 0; i < nums.length; i++) { let sum = 0 for (let j = i; j < nums.length; j++) { sum += nums[j] if (sum >= s) { min = Math.min(min, j - i + 1) if (min === 1) { return min } break } } } return min === Infinity ? 0 : min }
定义两个下标 i、j 为左右边界,中间的子数组为滑动窗口。在更新窗口的过程中不断的更新窗口之间的值的和 sum。
当 i 超出了数组的右边界,循环终止。
/** * @param {number} s * @param {number[]} nums * @return {number} */ let minSubArrayLen = function (s, nums) { let n = nums.length // 定义[i,...j]滑动窗口 取这个窗口里的和 let i = 0 let j = -1 let sum = 0 let res = Infinity while (i < n) { if (sum < s) { sum += nums[++j] } else { sum -= nums[i] i++ } if (sum >= s) { res = Math.min(res, j - i + 1) } } return res === Infinity ? 0 : res }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Uh oh!
There was an error while loading. Please reload this page.
209.长度最小的子数组
给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组,并返回其长度。如果不存在符合条件的连续子数组,返回 0。
示例:
进阶:
如果你已经完成了 O(n) 时间复杂度的解法, 请尝试 O(n log n) 时间复杂度的解法。
https://leetcode-cn.com/problems/minimum-size-subarray-sum/submissions
思路
暴力法(优化)
纯暴力的循环也就是穷举每种子数组并求和,当然是会超时的,这里就不做讲解了。下面这种解法会在暴力法的基础上稍作优化,具体的思路如下:
滑动窗口
定义两个下标 i、j 为左右边界,中间的子数组为滑动窗口。在更新窗口的过程中不断的更新窗口之间的值的和 sum。
当 i 超出了数组的右边界,循环终止。
The text was updated successfully, but these errors were encountered: